c# - 在表达式中插入节点/属性

标签 c# linq properties expression

我有一个表达式,我想在其中插入一个节点。我找到了 this SO question类似的问题,它在表达式的末尾添加了一个属性。 我对表达式很陌生,我不知道如何在节点之间执行此操作。

我做了这个简单的控制台测试应用程序来显示我遇到的问题:

public class Program
{
    private static void Main()
    {
        var provider = new Provider<Foo>();
        var foo = new Foo {Label = "myLabel", Owner = "Me"};
        provider.AddData(foo);

        provider.AddExpression(f => f.Label);

        // This writes: f => f.Label
        Console.WriteLine(provider.OriginalExpression.ToString());
        // This should write: f => f.WrappedData.Label
        // At the moment it writes: NULL
        Console.WriteLine(provider.WrapperExpression == null ? "NULL" : provider.WrapperExpression.ToString());

        Console.ReadKey();
    }
}

public class Foo
{
    public string Label { get; set; }
    public string Owner { get; set; }
}

public class Wrapper<T> where T : class
{
    public string Id { get; set; }
    public T WrappedData { get; set; }
}

public class Provider<T> where T : class
{
    public Wrapper<T> _internalWrapper = new Wrapper<T>();

    // The expression for the Property when using T (i.e. Foo)
    public Expression<Func<T, string>> OriginalExpression { get; private set; }
    // The expression for the Property when using _internalWrapper
    public Expression<Func<Wrapper<T>, string>> WrapperExpression { get; private set; }

    public void AddData(T data)
    {
        _internalWrapper = new Wrapper<T> { WrappedData = data, Id = "myId" };
    }

    public void AddExpression(Expression<Func<T, string>> valueExpression)
    {
        OriginalExpression = valueExpression;
        BuildWrapperExpression();
    }

    private void BuildWrapperExpression()
    {
        // HERE IS THE PROBLEM:
        // Here I have to insert the node "WrappedData" in the OriginalExpression and save it as WrapperExpression
        // So {f => f.Label} must result into {f => f.WrappedData.Label}
        // It should work as well for deeper nodes. I.e. when OriginalExpression is something like {f => f.Bar.Prop2.Label}
    }
}

我已经在 BuildWrapperExpression() 方法中尝试了一个 View 版本,但没有一个提供 f => f.WrappedData.Label . 我得到类似的东西 f => Invoke(value (ConsoleApplication1.Provide1+<>c__DisplayClass1[ConsoleApplication1.Foo]).lambda, f.WrappedData)或者 x => Invoke(f => f.Label, Invoke(e => e.WrappedData, x))

为了进一步使用表达式,必须是 x => x.WrappedData.Label

非常感谢各位

最佳答案

难道你不能简单地:

private void BuildWrapperExpression()
{
    var lambda = OriginalExpression.Compile();
    WrapperExpression = x => lambda(x.WrappedData);
}

或者,您可以实现 ExpressionVistor。查看 Marc 的回答:https://stackoverflow.com/a/9132775/1386995

关于c# - 在表达式中插入节点/属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19022790/

相关文章:

vba - 属性 vba 类的使用无效

ant - Sonar 测试和二进制属性,它们有什么作用?

c# - OpenXmlReader.Skip 应该如何工作?

c# - 未创建 MySQL Entity Framework 表

asp.net-mvc - Microsoft MVC 中的服务层和存储库设计

java - 如何在spring项目中读取属性文件?

c# - 使用具有整数 ID 的 Entity Framework 6.1(预发布)IdentityUser

c# - 在 SerialPort 上设置 DTR、RTS、CTS、DSR 和 Xonn/Xoff

c# - Enumerable.Skip 和排序

c# - 使用条件从另一个列表中删除列表项