Execute a method using the Action <T> argument using Reflection

How can I create an Action method to use as an argument for the next function?

public void When(Action<T> action) { if (internalValue != null) action(internalValue); } 

I have MethodInfo by method and parameter type, for example:

 var methods = value.GetType().GetMethods(); MethodInfo mInfo = methods.First(method => method.Name == "When"); Type parameterType = (mInfo.GetParameters()[0]).ParameterType; 

But after that I have no idea how to make the actual action method passed as an argument, I also don’t know how to define the body of the Action method.

+4
source share
1 answer
 mInfo.Invoke(value, delegate(<TheRuntimeTypeOf T> aTinstance) { // do something on a T }); 

But keep in mind that you are losing your pedigree.

+2
source

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


All Articles