且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

c# 在运行时创建未知的泛型类型

更新时间:2022-12-30 15:52:36

我认为您正在寻找 MakeGenericType 方法:

I think you're looking for the MakeGenericType method:

// Assuming that Property.PropertyType is something like List<T>
Type elementType = Property.PropertyType.GetGenericArguments()[0];
Type repositoryType = typeof(GenericRepository<>).MakeGenericType(elementType);
var repository = Activator.CreateInstance(repositoryType);