我想写一个名为IsExtensionMethod的方法,它的签名是:
public bool IsExtensionMethod(object anObject, string method)
{
// if method was an extension for anObject return true else false
}
我该怎么做?
我尝试使用以下代码,但在某些情况下,它的返回值不正确:
public bool IsExtensionMethod(object anObject, string method)
{
return anObject.GetType().GetMethods().Where(item => item.Name == method).Count() == 0;
}
答案 0 :(得分:2)
编译器在编译扩展方法时会向扩展方法添加CompilerServices.ExtensionAttribute
属性。寻找这个属性。
http://www.hanselman.com/blog/HowDoExtensionMethodsWorkAndWhyWasANewCLRNotRequired.aspx