Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using reflection to check if a method is "Extension Method"

As part of my application I have a function that receives a MethodInfo and need to do specific operations on it depending if that method is "Extension Method".

I've checked the MethodInfo class and I could not find any IsExtension property or flag that shows that the method is extension.

Does anyone knows how can I find that from the method's MethodInfo?

like image 325
Dror Helper Avatar asked Apr 06 '09 14:04

Dror Helper


2 Answers

You can call the IsDefined method on the MethodInfo instance to find this out by checking to see if the ExtensionAttribute is applied to the method:

bool isExtension=someMethod.IsDefined(typeof(ExtensionAttribute),true);
like image 97
Sean Avatar answered Oct 22 '22 12:10

Sean


Based on

F# extension methods in C#

it seems there is an attribute on the compiled form. So see if the method has this attribute:

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.extensionattribute.aspx

like image 44
Brian Avatar answered Oct 22 '22 12:10

Brian