I have a class that implements an interface like this;
... = new Location<ICafe>();
... = new Location<IBusiness>();
etc
If I want to expand the number of Location types I need to edit code at the moment.
Is there a way I can instantiate the class based on the string name of the interface?
So I would get Cafe or Business from the database and I’d like to instantiate the above using the string names of the interface.
edit
I should note that I use ICafe, IBusiness etc, later on in code to determine what type of item this is and what to display on screen.
Yes, reflection will let you do this, but there will be several steps:
Get a
System.Typecorresponding to the name, for exampleifaceType = Type.GetType(string)Get a
System.Typecorresponding to the generic type template, starting with any set of parameters, for examplegenericType = typeof (Location<ICafe>).GetGenericTypeDefinition()Combine the two:
specificType = genericType.MakeGenericType(ifaceType)Create an instance:
Activator.CreateInstance(specificType)