.NETGURU
How to create class instance automatically in C#
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.csharp.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Steven.Xu (VIP)
Hello everyone!
I have a problem about create an instance automatically in C#. Now I have a
class: namespace1.namespace11.CClass01. I want to create it's instance in a
function named CreateObjInstance. And the function not only generate just
CClass01. So I must sent the class type(whit it's namespace) into the
function.
There has two question following.
The first is I can't send a class type into function. It must is a object
instance. Can I do like that?
Secondly, How can I generate a class instance which not like: CClass01
newInstance = new CClass01. I need a CClass01 instance after I told the class
name to the function. Of course with some parameter of structure.

Thanks.
Reply to this message...
 
    
Nick Malik
Take a look at the Reflection classes. You can create objects by passing
the name of their type.

--- Nick

"Steven.Xu" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Steven.Xu (VIP)
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 1, 2)
As you know, the first parameter is error. because the namespace of class is
not an instance of System.Type. But i need to send the class type like this.
Do you know how to? Thanks!

Steven
Reply to this message...
 
    
Steven.Xu (VIP)
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 1, 2)
As you know, the first parameter is error. because the namespace of class is
not an instance of System.Type. But i need to send the class type like this.
Do you know how to? Thanks!

Steven
Reply to this message...
 
    
Daniel O'Connell [C# MVP] (VIP)
"Steven.Xu" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Try
CClass01 c = CreateObjInstance(typeof(namespace1.namespace11.CClass01),1,2);

The typeof operator returns the type for any given class name, including
namespaces.

> Steven

Reply to this message...
 
    
Daniel O'Connell [C# MVP] (VIP)
"Daniel O'Connell [C# MVP]" <Click here to reveal e-mail address> wrote in
message news:Click here to reveal e-mail address...
[Original message clipped]

Err, that is, a class *including* its namespace. A namespace on its own
doesn't have atype, it actually doesn't even exist as a seperate entity in
the runtime.
[Original message clipped]

Reply to this message...
 
    
Steven.Xu (VIP)
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 1, 2)
As you know, the first parameter is error. because the namespace of class is
not an instance of System.Type. But i need to send the class type like this.
Do you know how to? Thanks!

Steven
Reply to this message...
 
    
Steven.Xu (VIP)
Thank you Nick.
But I need to send a parameter as System.Type type. For example:
public BaseClass CreateObjInstance(System.Type typ, object[] a);
When I load this function as follow:
CClass01 c = CreateObjInstance(namespace1.namespace11.CClass01, 1, 2)
As you know, the first parameter is error. because the namespace of class is
not an instance of System.Type. But i need to send the class type like this.
Do you know how to? Thanks!

Steven
Reply to this message...
 
 
System.Type




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification