在泛型方法中获取 属性 的类型

Get type of property in generic method

我有方法:

static T RandomObject<T> (...) {
var tmp = Activator.CreateInstance<T>();
...
   foreach (PropertyInfo info in tmp.GetType().GetProperties()){
   ...
   }
}

如果 class T 有对象属性,我也想随机生成它们,所以我要做的是:

var t = info.PropertyType;
obj = RandomObject<t>(...);

然后我得到:

The type or namespace name t could not be found (are you missing a using directive or an assembly reference?)

有解决这个问题的办法吗?

尝试:

obj = GetType().GetMethod("RandomObject", BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(t).Invoke(null, ...);