English 中文(简体)
创造活力 另有两条(结合表达方式)的表述
原标题:Create dynamic Expression lambda from two others (chaining the Expressions)

Given a lambda that takes an Identification object, and returns a property:

Expression<Func<Identification, object>> fx = _ => _.Id;

将物体转换成身份识别证的Mlambda:

ParameterExpression p = Expression.Parameter(typeof(object), "o");
Expression @new = Expression.Lambda(Expression.Convert(p, typeof(Identification)), p);

我如何建造一个新的lam带,以安装@new(从身份证明中删除),并将结果通过<代码>fx。 页: 1 s 结果与<代码>fx第1参数有关,我找不到一个例子。

我需要将结果作为<代码>Expression,其中应当为:Expression<Func<object, Object>>,并将附限参数改为,然后取到。 Id property.

最佳回答

首先,请注意,如果你打上@new,就会更容易。 适当 i.e:

LambdaExpression @new = ...

since that provides easy access to @new.Body and @new.Parameters; that done, Expression.Invoke can be useful here:

var combinedParam = Expression.Parameter(typeof(object), "o");
var combined = Expression.Lambda<Func<object, object>>(
    Expression.Invoke(fx,
        Expression.Invoke(@new, combinedParam)), combinedParam);

尽管为了更清洁的表述,你还可以使用<条码>ExpressionVisitor,以完全取代内部表述:

var injected = new SwapVisitor(fx.Parameters[0], @new.Body).Visit(fx.Body);
var combined = Expression.Lambda<Func<object, object>>(injected,@new.Parameters);

class SwapVisitor : ExpressionVisitor {
    private readonly Expression from, to;
    public SwapVisitor(Expression from, Expression to) {
        this.from = from;
        this.to = to;
    }
    public override Expression Visit(Expression node) {
        return node == from ? to : base.Visit(node);
    }
}

这是:

  • inspect the fx.Body tree, replacing all instances of _ (the parameter) with the @new.Body (note that this will contain references to the o parameter (aka p)
  • we then build a new lambda from the replaced expression, re-using the same parameters from @new, which ensures that the values we injected will be bound correctly
问题回答




相关问题
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. ...

热门标签