English 中文(简体)
Pass 表达式树作为方法参数
原标题:Pass Expression Tree as Method parameter

我有一个函数:

public void Execute(Expression<Action> expression)
{
    var time = expression.Compile().Benchmark();

    var msg = string.Format("{0} took {1} to complete",
        ReflectionHelper.GetComponentCallDetails().ToString(),
        time.ToString(@"hh:mm:ss.ff"));

    Logger.Info(msg);
}

需要称呼的代表有:

channels = GetAllChannelsImpl(maxResults);

我对“表达树”比较新,

我能够用同样的功能

public void Execute(Action action)
{
    var time = action.Benchmark();

    var msg = string.Format("{0} took {1} to complete",
        ReflectionHelper.GetComponentCallDetails().ToString(),
        time.ToString(@"hh:mm:ss.ff"));

    Logger.Info(msg);
}

电话: 电话: 电话: 电话: 电话: 电话:

Execute(() =>
{
    channels = GetAllChannelsImpl(maxResults);
});

但我想使用基于“表达树”的方法, 来消除使用反省间接费用 找出记录方法细节的必要性。

任何人都可以建议将上述行动代表通过表达树的正确方式作为方法参数。

最佳回答

羔羊表达方式本身没有类型。 它所选的实际类型由编译者根据您想要指派或投放的内容来推断。 上面说, 使用简单的羔羊使用 < code> Execute () 方法的任何呼叫都将模糊不清, 因为您的羊羔将兼容为 < code> Action 或 < code> Expression< Action> 。 您需要通过明确投放到您期望的类型来解析它 。

// assign to a variable
Expression<Action> action1 = () => ...;
Execute(action1);

// cast
Execute((Expression<Action>)(() => ...));

// use the constructor
Execute(new Expression<Action>(() => ...));

IMHO最好删除模糊的超载并重命名其中一种方法。 我建议将表达式超载改名为ExecuteExpression ()

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签