如何获取表达式的值

发布于 2024-11-28 13:20:26 字数 624 浏览 0 评论 0原文

我有一个使用 Linq 表达式的函数:

private Expression GetFieldValueExpression(ParameterExpression parameter, string fieldName)
{
  Expression properyIndexExpression = System.Linq.Expressions.Expression.Constant (fieldName, typeof(string));
  IndexExpression fieldValueExpression = System.Linq.Expressions.Expression.Property(parameter, "Item", new Expression[] { properyIndexExpression });
  return Expression.Property(fieldValueExpression, "Value");
}

Expression.Property(fieldValueExpression, "Value") 返回的值是字符串类型。

我不知道如何得到它。我知道我必须创建一个 lambda 并编译它,但我不知道如何做。

谢谢您的宝贵时间。

I have this function that uses Linq expressions:

private Expression GetFieldValueExpression(ParameterExpression parameter, string fieldName)
{
  Expression properyIndexExpression = System.Linq.Expressions.Expression.Constant (fieldName, typeof(string));
  IndexExpression fieldValueExpression = System.Linq.Expressions.Expression.Property(parameter, "Item", new Expression[] { properyIndexExpression });
  return Expression.Property(fieldValueExpression, "Value");
}

The value returned by Expression.Property(fieldValueExpression, "Value") is of type string.

I don't know how to get it. I know that i must create a lambda and compile it, but i don't know how.

Thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘心安 2024-12-05 13:20:26

也许您正在寻找这样的代码:

    public void EvaluateAnExpression()
    {
        //Make the parameter
        var parm = Expression.Parameter(typeof(TestClass),"parm");

        //Use your method to build the expression
        var exp = GetFieldValueExpression(parm, "testField");

        //Build a lambda for the expression
        var lambda = Expression.Lambda(exp, parm);

        //Compile the lamda and cast the result to a Func<>
        var compiled = (Func<TestClass, string>)lambda.Compile();

        //We'll make up some object to test on
        var obj = new TestClass();

        //Get the result (it will be TESTFIELD)
        var result = compiled(obj);
    }

该代码假设一些如下所示的测试类(基本上索引器属性仅返回输入,但为大写 - 一个简单的示例,但适用于测试):

    public class TestClass
    {
        public InnerClass this[string indexParameter]
        {
            get
            {
                return new InnerClass { Value = indexParameter.ToUpper() };
            }
        }
    }

    public class InnerClass
    {
        public string Value { get; set; }
    }

Perhaps you are looking for code like this:

    public void EvaluateAnExpression()
    {
        //Make the parameter
        var parm = Expression.Parameter(typeof(TestClass),"parm");

        //Use your method to build the expression
        var exp = GetFieldValueExpression(parm, "testField");

        //Build a lambda for the expression
        var lambda = Expression.Lambda(exp, parm);

        //Compile the lamda and cast the result to a Func<>
        var compiled = (Func<TestClass, string>)lambda.Compile();

        //We'll make up some object to test on
        var obj = new TestClass();

        //Get the result (it will be TESTFIELD)
        var result = compiled(obj);
    }

that code assumes some test classes that look like this (basically the indexer property just returns the input but in upper case - a trivial example but works for testing):

    public class TestClass
    {
        public InnerClass this[string indexParameter]
        {
            get
            {
                return new InnerClass { Value = indexParameter.ToUpper() };
            }
        }
    }

    public class InnerClass
    {
        public string Value { get; set; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文