0

I'm building this expression tree:

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

With this code:

 _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;
    }

But I receive NullReferenceException when evaluating it. Can anybody help?

Archeg
  • 8,364
  • 7
  • 43
  • 90
  • Parse the stack trace and see where's the problem. – Alvin Wong Nov 19 '12 at 15:17
  • @AlvinWong It says nothing. Here is the part of it: at lambda_method(Closure ) at Remotion.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.EvaluateSubtree(Expression subtree) at Remotion.Linq.Parsing.ExpressionTreeVisitors.PartialEvaluatingExpressionTreeVisitor.VisitExpression(Expression expression) at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitLambdaExpression(LambdaExpression expression) at Remotion.Linq.Parsing.ExpressionTreeVisitor.VisitExpression(Expression expression) at ... – Archeg Nov 19 '12 at 15:29

1 Answers1

1

Found the problem. It appears the expression works perfectly - that's the evaluator who behaves wrong. I'm passing built expression to NHibernate and it cannot built sql from this expression.

It appeared that Dictionary<string, object>() { {"key", "value"}} isn't the same as var dict = new Dictionary<string, object>(); dict.Add("key", "value"); return dict. When I translated my expression two the first variant - that worked and now I'm able to construct dynamic queries to NHibernate.

An example builder that I'm using to those who are interested:

// 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;
Archeg
  • 8,364
  • 7
  • 43
  • 90