Re: activator classes n createInstance()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Skeet [C# MVP]

    Re: activator classes n createInstance()

    On Apr 11, 8:40 am, Andrew <And...@discuss ions.microsoft. comwrote:
    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("S ystem.Math");
    Type testType = Type.GetType(na me);
    Type testType2 = Type.GetType("n ame");
    >
    --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.Creat eInstance(theMa thType); --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.Creat eInstance(testT ype); --Error: Value
    cannot be
    null. Parameter name: type.
    Object theObj4 = Activator.Creat eInstance(testT ype2); --Error: Value
    cannot be
    null. Parameter name: type.
    Well yes - you've failed to find the type, so Activator.Creat eInstance
    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
Working...