复制代码 [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries"), SecurityCritical]
internal static ObjectHandleOnStack GetObjectHandleOnStack<T>(ref T o) where T: class
{
TypedReference reference = __makeref(o);
return new ObjectHandleOnStack(reference.GetPointerOnStack());
}
这里将Type转换成了TypedReference。关键大约就是这里,可惜我无法找到typeof()的具体实现方式。代码追踪到这里,就无法判断这里发生的真实原因了。若要了解.NET底层机制的同学,可以告诉我。
若要解决反射方式无法通过IsDefined()判断的问题,可以调用MethodInfo的GetCustomAttribute()方法。例如:
复制代码 private static bool IsAppliedWith(this MethodInfo methodInfo, Type attributeType, string attributeName)
{
return methodInfo.GetCustomAttributes(attributeType, false).ToString().Contains(attributeName);
}
无论是利用反射加载,还是使用typeof,采用这种方式判断方法是否运用了指定的Attribute,都是能够生效的。
以上就是C#IsDefined的问题的全部内容,希望能给大家一个参考,也希望大家多多支持ASPKU。
注:相关教程知识阅读请移步到c#教程频道。










