让我说我有这个课:
class Test123<T> where T : struct
{
public Nullable<T> Test {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
和这堂课
class Test321
{
public Test123<int> Test {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
所以对于这个问题,我想说我想通过反射创建一个Test321并用一个值设置"Test"如何获得泛型类型?
Mar*_*ell 15
从您开始Test321,获取类型的最简单方法是从属性:
Type type = typeof(Test321);
object obj1 = Activator.CreateInstance(type);
PropertyInfo prop1 = type.GetProperty("Test");
object obj2 = Activator.CreateInstance(prop1.PropertyType);
PropertyInfo prop2 = prop1.PropertyType.GetProperty("Test");
prop2.SetValue(obj2, 123, null);
prop1.SetValue(obj1, obj2, null);
Run Code Online (Sandbox Code Playgroud)
或者你的意思是你想找到T?
Type t = prop1.PropertyType.GetGenericArguments()[0];
Run Code Online (Sandbox Code Playgroud)