I have an abstract class with a few derived class
public abstract class MyObject
{
public string name { get; set; }
public bool IsObject(string pattern);
Yes, use Reflection.
You can use Type.GetType to get an instance of the Type
for the class by string, then instantiate it using Activator.CreateInstance
, something like this:
public MyObject Create(string pattern)
{
Type t = Type.GetType(pattern);
if (t == null) {
throw new Exception("Type " + pattern + " not found.");
}
return Activator.CreateInstance(t);
}
You could use Activator.CreateInstance(string, string)
overload also, but this would not directly return a new instance of the Type
required.