I want to write a method named IsExtensionMethod that its signature is :
public bool IsExtensionMethod(object anObject, string method)
{
// if method was an extension for anObject return true else false
}
How can i do it?
I tried for following code but in some cases it’s return value is incorrect:
public bool IsExtensionMethod(object anObject, string method)
{
return anObject.GetType().GetMethods().Where(item => item.Name == method).Count() == 0;
}
The compiler adds a
CompilerServices.ExtensionAttributeattribute to extension methods when it compiles them. Look for this attribute.http://www.hanselman.com/blog/HowDoExtensionMethodsWorkAndWhyWasANewCLRNotRequired.aspx