none
Create instance of Class with a string RRS feed

  • Question

  • I would like to create instance of User Control using string as type name.

    example,

    "MyForm1" is a User Control in namespace "MyApp.UserControls" (Physically is MyForm1.xaml in '/root/UserControls' folder)

    Similar for MyForm2, MyForm3.

    In MainWindow.xaml (in '/root/'), I will get a string from user and open the corresponding form

    Normally, we use MyForm1 form1 = new MyForm1(); to create the form

    but I want something like below, but it does not work.

    string formName = GetFormName();  (this will be either MyForm1 or MyForm2 or MyForm3)

    a) System.Activator.CreateInstance("MyApp", formName);

    b) System.Activator.CreateInstance(null, formName);

    c) System.Activator.CreateInstance("MyApp.UserControls", formName);

    I am using CreateInstance but it prompts error in the above 3 cases, any comments? Thanks.


    • Edited by swivan Wednesday, April 17, 2013 10:22 AM
    Wednesday, April 17, 2013 10:21 AM

Answers

  • The first CreateInstance parameter must be the assembly name. That's probably "MyApp" in your case but cases b & c are wrong.

    The second CreateInstance parameter must be the type full name. In your case that's MyApp.UserControls.Form1 since the full name includes the namespace too.

    So you should use something like:

    Activator.CreateInstance("MyApp", "MyApp.UserControls." + formName)

    That said, I would think twice before using a string provided by the user like this. What if you want to translate the string in another language? Of what if the user type the wrong name that happens to match the name of another class in your project? In general this should be done only if your users are technical persons and they understand that the name they provide is the name of a class.

    • Marked as answer by swivan Thursday, April 18, 2013 1:08 AM
    Wednesday, April 17, 2013 11:10 AM
    Moderator

All replies

  • The first CreateInstance parameter must be the assembly name. That's probably "MyApp" in your case but cases b & c are wrong.

    The second CreateInstance parameter must be the type full name. In your case that's MyApp.UserControls.Form1 since the full name includes the namespace too.

    So you should use something like:

    Activator.CreateInstance("MyApp", "MyApp.UserControls." + formName)

    That said, I would think twice before using a string provided by the user like this. What if you want to translate the string in another language? Of what if the user type the wrong name that happens to match the name of another class in your project? In general this should be done only if your users are technical persons and they understand that the name they provide is the name of a class.

    • Marked as answer by swivan Thursday, April 18, 2013 1:08 AM
    Wednesday, April 17, 2013 11:10 AM
    Moderator
  • It is solved using

    Activator.CreateInstance(null, "MyApp.UserControls." + formName)

    And thanks for your advise, the user is selected from a list only so lanuage and wrong name issue would not occur, thanks.

    Thursday, April 18, 2013 1:10 AM