Creating generic variables from a type - How? Or use Activator.CreateInstance() with properties { } instead of parameters ( )?

前端 未结 3 1278
耶瑟儿~
耶瑟儿~ 2021-01-18 10:27

I\'m currently using Generics to make some dynamic methods, like creating an object and filling the properties with values.

Is there any way to \"dynamically\" creat

3条回答
  •  青春惊慌失措
    2021-01-18 10:48

    You can use MakeGenericType:

    Type openListType = typeof(List<>);
    Type genericListType = openListType.MakeGenericType(obj.GetType());
    object instance = Activator.CreateInstance(genericListType);
    

提交回复
热议问题