评估动态构建的表达式树时出现NullReferenceException

时间:2012-11-19 15:15:51

标签: c# .net nhibernate linq-expressions

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

.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
  }
}

使用此代码:

 _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。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

发现问题。看起来表达式完美 - 这是行为错误的评估者。我将构建的表达式传递给NHibernate,它不能从这个表达式构建sql。

Dictionary<string, object>() { {"key", "value"}}似乎与var dict = new Dictionary<string, object>(); dict.Add("key", "value"); return dict不同。当我翻译我的表达式时,第一个变体 - 这是有效的,现在我能够为NHibernate构建动态查询。

我对那些感兴趣的人使用的示例构建器:

// 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;