activator classes n createInstance()

A

Andrew

Hi,

I've looked at the Activator classes and I've come out with these samples of
code. However there is an error:

e.g. codes:
=======
string name = "ClassABC";

Type theMathType = Type.GetType("System.Math");
Type testType = Type.GetType(name);
Type testType2 = Type.GetType("name");

--> both testType and testType2 gives "null".

Object theObj2 = Activator.CreateInstance(theMathType); --> Error: Cannot

create an abstract class.
Object theObj3 = Activator.CreateInstance(testType); --> Error: Value
cannot be

null. Parameter name: type.
Object theObj4 = Activator.CreateInstance(testType2); --> Error: Value
cannot be

null. Parameter name: type.

I'm not very familiar with CreateInstance method etc ...
I just want to create an instance of "name" which is the name of the class
that I want to instantiate.

Thanks

regards,
Andrew
 
J

Jon Skeet [C# MVP]

Hi,

I've looked at the Activator classes and I've come out with these samples of
code. However there is an error:

e.g. codes:
=======
string name = "ClassABC";

Type theMathType = Type.GetType("System.Math");
Type testType = Type.GetType(name);
Type testType2 = Type.GetType("name");

--> both testType and testType2 gives "null".

Okay, that means you've either not given the full classname (including
namespace) or it's not in mscorlib or the currently executing
assembly, in which case you need to give assembly details as well.

The line with testType2 is explicitly trying to find a class called
"name". The string "name" and the variable called name are completely
independent things.
Object theObj2 = Activator.CreateInstance(theMathType); --> Error: Cannot
create an abstract class.

That's natural - try compiling this:

Math m = new System.Math();

and you'll have the same kind of issue.
Object theObj3 = Activator.CreateInstance(testType); --> Error: Value
cannot be
null. Parameter name: type.
Object theObj4 = Activator.CreateInstance(testType2); --> Error: Value
cannot be
null. Parameter name: type.

Well yes - you've failed to find the type, so Activator.CreateInstance
can't possibly create an instance of it.
I'm not very familiar with CreateInstance method etc ...
I just want to create an instance of "name" which is the name of the class
that I want to instantiate.

Is it in the same assembly as your calling code? If so, it's probably
just a case of putting the namespace on as well.

Jon
 
A

Andrew

Found it.

string name = Properties.Settings.Default.ClassName.ToString();

//"myproject.myclass, myassembly" format.
//name = "ABC.MyClass, Assem" ;
Type t = Type.GetType(name);

Object obj = Activator.CreateInstance(t);
string result = t.InvokeMember("methodName",
System.Reflection.BindingFlags.InvokeMethod, null, obj, new object[] {
strA,
strB });

cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top