Apologies for the awful title, I couldn't think of a better way to phrase it.
Essentially I have an abstract base class Category
and whole host of derived classes inheriting from that. Is it possible to do something along the lines of:
List<Category> catList = new List<Category>();
catList.Add(new Category(myCategoryTypeString));
Where myCategoryTypeString
will always be the name of one of the derived classes and have it instead create a new object of that derived class (presumably using a switch on the string to determine which class to use).
So that something along the lines of:
catList.Add(new Category("Engineering"));
Would add a new object of Engineering : Category
type to the list?
If that is possible how would one go about doing this? My abstract class is defined thusly:
abstract class Category
{
private string line;
private bool ToChallenge;
private string[] columns;
private int oppLevel;
protected Category(string line)
{
this.line = line;
columns = line.Split(',');
}
public abstract bool IsAnomoly();
public abstract void CategoriseAnomoly();
public abstract void WriteLine();
}
No, you can't do that - if you call new Foo()
, that will always create an instance of Foo
, never of a subclass.
Instead, create a static factory method in Category
:
public static Category CreateCategory(string line)
{
if (line == "Engineering")
{
return new EngineeringCategory();
}
// Whatever you want to do for non-engineering categories
}
As noted in comments, using the actual type name probably isn't a great idea, unless this is already machine-generated output. If you really want to, you can use something like:
public static Category CreateCategory(string typeName)
{
// Note that the type name would have to be namespace-qualified here
Type type = Type.GetType(typeName);
return (Category) Activator.CreateInstance(type);
}
See more on this question at Stackoverflow