locked
Initialize an object from string type RRS feed

  • Question

  • Hello!

    I have list of strings with class types on this format:

    "MyProject.Classes.MyClass"

    How can I instantiate objects of these classes on runtime. Do I have to use reflection? I'm not familiar with that so unsure of the usage.

     

    So I would like something like this

    var MyObject = new TheCorrectClassBasedOnTheString();

     

    Best regards

    Ola

    Wednesday, December 1, 2010 11:35 AM

Answers

  • Hi,

    yes, you need a little bit of reflection.

    First you get the Type itself. For that, you can use Type.GetType(String) or Type.GetType(String, Boolean) function.

    The next step is, to get the constructor: yourType.GetConstructor(...). That will give you a ConstructorInfo object and you can call the Method Invoke on it.

    Please see the msdn documentation of the given classes / methods to find more details and examples.

    With kind regards,

    Konrad

    • Marked as answer by halola Wednesday, December 1, 2010 12:18 PM
    Wednesday, December 1, 2010 11:51 AM
  • You do not need to get the constructor. Just use Activator.CreateInstance Method and pass the type you have.
    MCTS, CodeProject MVP 2008
    • Proposed as answer by Konrad Neitzel Wednesday, December 1, 2010 12:37 PM
    • Marked as answer by halola Wednesday, December 1, 2010 12:42 PM
    Wednesday, December 1, 2010 12:18 PM

All replies

  • Hi,

    yes, you need a little bit of reflection.

    First you get the Type itself. For that, you can use Type.GetType(String) or Type.GetType(String, Boolean) function.

    The next step is, to get the constructor: yourType.GetConstructor(...). That will give you a ConstructorInfo object and you can call the Method Invoke on it.

    Please see the msdn documentation of the given classes / methods to find more details and examples.

    With kind regards,

    Konrad

    • Marked as answer by halola Wednesday, December 1, 2010 12:18 PM
    Wednesday, December 1, 2010 11:51 AM
  • Hello Konrad, Thanks for the quick answer!

    I am able to get the Type, but not the contstructor. Can you check this code, and see if I missed something?

    Type oType = Type.GetType(objectTypeString);
    var constructor = oType.GetConstructor(new Type[] { oType }); // constructor is null after this line
    var s = j.Invoke(null); // what parameter should i use here?
    

    I just want to get the default constructor.

    I will check the MSDN documentation on this also.

    Thank you

    Ola

    Wednesday, December 1, 2010 12:10 PM
  • You do not need to get the constructor. Just use Activator.CreateInstance Method and pass the type you have.
    MCTS, CodeProject MVP 2008
    • Proposed as answer by Konrad Neitzel Wednesday, December 1, 2010 12:37 PM
    • Marked as answer by halola Wednesday, December 1, 2010 12:42 PM
    Wednesday, December 1, 2010 12:18 PM
  • I found another way to initiate the object when I have gotten the type.

     

    Type oType = Type.GetType(objectTypeString);
    var myObject = Activator.CreateInstance(oType);
    

     

    This worked for me. Thanks for leading me in the right direction Konrad!

    Ola

    Wednesday, December 1, 2010 12:21 PM
  • Ahh great. I always used the other way I described. In the code, you tried is a simple error:
    GetConstructor gets the types that are the arguments for the Constructor. So instead of
    var constructor = oType.GetConstructor(new Type[] { oType });
    you should try:
    var constructor = oType.GetConstructor(new Type[] { });

    But the Activator Class is very interesting. Thank you Giorgi to point to this possibility!

    With kind regards,

    Konrad

    Wednesday, December 1, 2010 12:36 PM