首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算动态构建的表达式树时的NullReferenceException

计算动态构建的表达式树时的NullReferenceException
EN

Stack Overflow用户
提问于 2012-11-19 23:15:51
回答 1查看 435关注 0票数 0

我正在构建这个表达式树:

代码语言:javascript
复制
.Lambda 
#Lambda1<System.Func`2[RTX.Ulysses.TestFramework.TestCaseDataResult,
System.Collections.Generic.Dictionary`2[System.String,System.Object]]>
(RTX.Ulysses.TestFramework.TestCaseDataResult $x)
{
.Block(
    System.Collections.Generic.Dictionary`2[System.String,System.Object] $dict,
    RTX.Ulysses.TestFramework.TestCaseDataResult $x) {
    .Block(
        System.Collections.Generic.Dictionary`2[System.String,System.Object] $dict,
        RTX.Ulysses.TestFramework.TestCaseDataResult $x) {
        $dict = .New 
System.Collections.Generic.Dictionary`2[System.String,System.Object]();
        .Call $dict.Add(
            "Id",
            (System.Object)$x.Id)
    };
    $dict
  }
}

使用以下代码:

代码语言:javascript
复制
 _parameterExpression = Expression.Parameter(typeof(TestCaseDataResult), "x");
 // Init
        //Expression valExpression = Expression.Property(parameter, "Length");
        ParameterExpression dictVar = Expression.Variable(typeof(Dictionary<string, object>), "dict");
        Expression newDict = Expression.New(typeof(Dictionary<string, object>).GetConstructors()[0]);
        Expression builtExpression = Expression.Assign(dictVar, newDict);

        // Adding a value
        List<Expression> calls = new List<Expression>();
        calls.Add(builtExpression); // Add variable initialization.
        foreach (var fieldPath in dictionaryToBuild)
        {
            Expression valExpression = BuildLambda(fieldPath);
            calls.Add(Expression.Call(dictVar, typeof (Dictionary<string, object>).GetMethod("Add"),
                                      Expression.Constant(fieldPath),
                                      Expression.Convert(valExpression, typeof (object))));
        }

        builtExpression = Expression.Block(new List<ParameterExpression>() { dictVar, _parameterExpression }, calls); // dictVar, _parameterExpression
        builtExpression = Expression.Block(typeof(Dictionary<string, object>), new List<ParameterExpression>() { dictVar, _parameterExpression }, builtExpression, dictVar);

        Expression<Func<TestCaseDataResult, Dictionary<string, object>>> finalExpression = Expression.Lambda<Func<TestCaseDataResult, Dictionary<string, object>>>(builtExpression, _parameterExpression);

        return finalExpression;

public Expression BuildLambda(string expressionString)
    {
        Expression builtExpression = _parameterExpression;
        foreach (var part in expressionString.Split('.'))
        {
            builtExpression = Expression.Property(builtExpression, part);
        }

        return builtExpression;
    }

但我在评估它时会收到NullReferenceException。有人能帮上忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-19 23:57:26

找到了问题所在。看起来这个表达式工作得很好--这就是行为错误的求值器。我正在将构建的表达式传递给NHibernate,但是它不能从这个表达式构建sql。

看起来Dictionary<string, object>() { {"key", "value"}}var dict = new Dictionary<string, object>(); dict.Add("key", "value"); return dict是不一样的。当我翻译我的表达式两个,第一个变体-工作了,现在我能够构造动态查询到NHibernate。

我给感兴趣的人使用的一个示例构建器:

代码语言:javascript
复制
// Init 
        NewExpression newDict = Expression.New(typeof(Dictionary<string, object>));

        // Adding a value
        List<ElementInit> elements = new List<ElementInit>();

        System.Reflection.MethodInfo addMethod = typeof(Dictionary<string, object>).GetMethod("Add");
        foreach (var fieldPath in dictionaryToBuild)
        {
            Expression valExpression = BuildLambda(fieldPath);
            elements.Add(Expression.ElementInit(addMethod, Expression.Constant(fieldPath), Expression.Convert(valExpression, typeof(object))));
        }

        var listInitExpression = Expression.ListInit(newDict, elements);

        Expression<Func<TestCaseDataResult, Dictionary<string, object>>> finalExpression = Expression.Lambda<Func<TestCaseDataResult, Dictionary<string, object>>>(listInitExpression, _parameterExpression);

        return finalExpression;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13456656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档