简体   繁体   中英

C# passing generic functions as parameters

I'm trying to audit method calls from within the application so that I can profile and analyze the parameters. I got this to work,

Run( ()=> SomeFunction(paramA, paramB), paramA, paramB) );

public static void Run(Expression<Action> action, params object[] param)
{
   var func = action.Compile();
   func();
}

public static T Run<T>(Expression<Func<T>> action, params object[] param)
{
   var func = action.Compile();
   return func();
}

However I was hoping to lose the lambda and make the method even easier to use. For example,

Run(SomeFunc, paramA, paramB);

I got to here, but I'm stumped, in case someone can help me out:

private T Run<T>(Func<T> subReturn) {}

I don't understand what you want exactly, but if I understand it correctly, than this might help:

private T Run<T, TLeft, TRight>(Func<TLeft, TRight, T> action, TLeft leftParam, TRight rightParam)
{
    return action(leftParam, rightParam);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM