简体   繁体   English

C#将泛型函数作为参数传递

[英]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. 但是我希望失去lambda并使该方法更容易使用。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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