ReflectorMemberOf(ExpressionAction) Method

Gets the action method of an expression by a refactoring-safe way.

Definition

Namespace: KGySoft.Reflection
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 10.0.0
C#
public static MethodInfo MemberOf(
	Expression<Action> expression
)

Parameters

expression  ExpressionAction
An expression accessing an action method.

Return Value

MethodInfo
The MethodInfo instance of the expression.

Remarks

Similarly to the typeof operator, which provides a refactoring-safe reference to a Type, this method provides a non-string access to an action method:

Examples

C#
MethodInfo methodAdd = Reflector.MemberOf(() => default(List<int>).Add(default(int))); // MethodInfo: List<int>.Add() - works without a reference to a List

To reflect a function method, constructor, property or a field, you can use the MemberOfT(ExpressionFuncT) method.

To reflect methods, you can actually cast the method to a delegate and get its Method property:

Examples

C#
MethodInfo methodAdd = ((Action<int>)new List<int>().Add).Method; // MethodInfo: List<int>.Add() - a reference to a List is required

  Note

Accessing a method by the delegate cast is usually faster than using this method. However, you must have an instance to access instance methods. That means that you cannot use the default operator for reference types to access their instance methods. If the constructor of such a type is slow, then using this method can be more effective to access an instance method.

Exceptions

ArgumentNullExceptionexpression is .
ArgumentExceptionexpression does not access an action method.

See Also