I have a parent class and several inherited from it
For some actions, I get a string with the name of the class
Then get class from this string
CommonModel model = GetModelFromString(modelType);
Then I need to create generic
Actions<T> actions = new Actions<T>();
I need replace T
to model
. But model is var not a class.
How can I pass the class to generic? (Is it possible at all?)
Generic
public class Actions<T>
where T: CommonModel
{
}
Works for me
Type generic = typeof(Actions<>); //generic without <T>
var model = GetModelFromString(modelType); //string model that must be in <>
Type[] typeArgs = { model.GetType() };
Type constructed = generic.MakeGenericType(typeArgs);
var myObject = Activator.CreateInstance(constructed);
var b = (Actions<Materials>)myObject; //upcast to see methods