1

Iam trying to create instances of classes by string names. Iam creating utility where user chose type from popup box of string (content of this field is content of field "types") and I need to create instance of class based on his choice. Unfortunatelly I totally dont know how to do it

class Parent
{

}

class Child1 : Parent
{

}

class Child2 : Parent
{

}

string[] types = { "Child1", "Child2" };
List<Parent> collection = new List<Parent>(); 

void Main()
{
    Parent newElement = Activator.CreateInstance(this.types[0]) as Parent;  // this row is not working :( and I dont know how to make it work

    this.collection.Add(newElement);
    if (this.collection[0] is Child1)
    {
        Debug.Log("I want this to be true");
    }
    else
    {
        Debug.Log("Error");
    }
}

I finnaly make it work. Thank you all. Here is working code (problem was in missing namespace)

namespace MyNamespace

{ class Parent {

}

class Child1 : Parent
{

}

class Child2 : Parent
{

}

class Main
{
    string[] types = { typeof(Child1).ToString(), typeof(Child2).ToString() };
    List<Parent> collection = new List<Parent>(); 

    public void Init()
    {
        Parent newElement = Activator.CreateInstance(Type.GetType(this.types[0])) as Parent;

        this.collection.Add(newElement);
        if (this.collection[0] is Child1)
        {
            Debug.Log("I want this to be true");
        }
        else
        {
            Debug.Log("Error");
        }
    }
}

}

3
  • When you say "this row is not working", are you able to identify any particular way in which it doesn't work? Does it not compile, does it throw an exception, does it return null, or does it just make the frowny face that you mention in your question? Did you look up the frowny face in MSDN and see if there was anything helpful about it? Commented Jun 28, 2016 at 17:21
  • This row is not working completly (not compile) I put it here only for illustration what I tryed. I googled it a lot and I found, that this problem can be solved by Activator.CreateInstance but I cant make it work. Currently Iam looking for completly new idela how to solve my problem. Commented Jun 28, 2016 at 17:26
  • 1
    include the namespace in your code, because that's what you are missing. Commented Jun 28, 2016 at 17:40

2 Answers 2

2

You need to provide a namespace for your classes:

string[] types = { "MyApplication.Child1", "MyApplication.Child2" };

Then, you can create an instance using the actual type:

Parent parent = Activator.CreateInstance(Type.GetType(this.types[0]));
Sign up to request clarification or add additional context in comments.

2 Comments

I already tryed it but it allways give me error ArgumentNullException: Argument cannot be null.
@MrIncognito update your question with the namespace you used and whatever you have tried.
1

The Activator.CreateInstance method does not take a string as a parameter. You need to provide a Type.

Type parentType = Type.GetType(types[0],false); //param 1 is the type name.  param 2 means it wont throw an error if the type doesn't exist

Then check to see if the type is found before using it

if (parentType != null)
{
    Parent newElement = Activator.CreateInstance(parentType) as Parent;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.