Is there any way to determine if this parameter has this modifier?

I would like to determine if a parameter has a modifier thisusing Reflection. I looked at the class properties ParameterInfobut could not find anything useful. I know that extension methods are just syntactic sugars, but I believe there should be a way to determine if a method is an extension method.

The only thing that distinguishes extension methods from other static methods (which are defined in a static public class) is the modifier this.

For example, this is not an extension method:

public static int Square(int x) { return x * x; }

But this:

public static int Square(this int x) { return x * x; }

So, how can I distinguish between two methods, using Reflectionor something else, if possible?

+4
1

, , ExtensionAttribute.

var method = type.GetMethod("Square");
if (method.IsDefined(typeof(ExtensionAttribute), false))
{
    // Yup, it an extension method
}

, , :

[Extension]
public static int Square(int x) { return x * x; }

... - . , , ( ), , this .

+7

Source: https://habr.com/ru/post/1541770/


All Articles