我需要通过反射获取泛型参数的泛型类型。而不是类型{ Name="T“;FullName=null }
public void Sample<T>(T i, object o)
{
MethodBase basee = MethodBase.GetCurrentMethod();
Type[] types = basee.GetGenericArguments();
}但是我不能使用typeof(T),因为我使用的是反射
有没有办法(使用反射)来获取泛型参数的类型。
// in this case i should get (Type) { Name="Int32" ; FullName="System.Int32" }
myClassObj.Sample(10, new object());另外,我想知道如何调用typeof(T) ex调用的方法:
// in Il code I see that the compiler use:
// ldtoken !!T => this should get the RuntimeTypeHandle needed as parameter
// System.Type::GetTypeFromHandle(valuetype System.RuntimeTypeHandle)
Type t = Type.GetTypeFromHandle( ? ? ? ); 如何从T泛型参数中获取RuntimTypeHandle
发布于 2012-09-08 12:06:17
我不知道这是什么意思,但是
public void Sample<T>(T i, object o)
{
Type t = typeof(T);
if (i != null)
{
t = i.GetType();
}
// Do whatever with t
Console.WriteLine(t);
}然后你就得到了运行时类型(如果有一个对象),否则你就得到了静态类型。
https://stackoverflow.com/questions/12327834
复制相似问题