How to create class instance automatically in C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
Take a look at the Reflection classes. You can create objects by passing
the name of their type.

--- Nick
 
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
 
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
 
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
 
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
 
Steven.Xu said:
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!

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

The typeof operator returns the type for any given class name, including
namespaces.
 
Daniel O'Connell said:
Steven.Xu said:
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!

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

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

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.
 
Back
Top