ReflectorMemberOf(ExpressionAction) Method
Gets the action method of an expression by a refactoring-safe way.
Namespace: KGySoft.ReflectionAssembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 10.0.0
public static MethodInfo MemberOf(
Expression<Action> expression
)
Public Shared Function MemberOf (
expression As Expression(Of Action)
) As MethodInfo
public:
static MethodInfo^ MemberOf(
Expression<Action^>^ expression
)
static member MemberOf :
expression : Expression<Action> -> MethodInfo
- expression ExpressionAction
- An expression accessing an action method.
MethodInfoThe
MethodInfo instance of the
expression.
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:
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:
MethodInfo methodAdd = ((Action<int>)new List<int>().Add).Method; // MethodInfo: List<int>.Add() - a reference to a List is required
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.