I am having issues creating generic class instance. This is my code, please take a look at the remark. This is where the error ocurs. I want to put different types into a list and create instances according to the types that the user defined in the application. How can I achieve that ? I know how to use reflection with Activator but I don't know how can I create the generic class at runtime.
public class Field<T> : IField
{
public string Name { get; set; }
public Type Type => typeof(T)
}
IEnumerable<Type> types = new List<Type>{typeof(int),typeof(string)}
foreach(var type in types)
{
var instance = new Field<type>() //error
}
Aucun commentaire:
Enregistrer un commentaire