tags:

views:

56

answers:

1

How can i create a Action method to use as a argument to the following function?

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

I have the MethodInfo on the method, and the parameter type like so:

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 to pass as argument, i also do not know how to define the Action method body.

+1  A: 
mInfo.Invoke(value,
    delegate(<TheRuntimeTypeOf T> aTinstance)
    {
        // do something on a T
    });

But keep in mind that you're loosing the genericity.

Seb