In this expression tree, why am I getting a null reference exception?

.net c# c#-4.0 expression-trees lambda

Question

I have the following tree expression:

.Block(
    System.Object $instance,
    MyType2 $result) {
    $result = (MyType2)((MyType1)$instance).Property1;
    .Goto return { };
    .Label
    .LabelTarget useDefault:;
    $result = .Default(MyType2);
    .Label
    .LabelTarget return:;
    $result
}

The expression tree uses the following custom types:

public class MyType1
{
    public MyType2 Property1 { get; set; }
}

public class MyType2
{
}

The expression tree is built, compiled, and invoked in the following manner (although it won't function precisely as it does since I've omitted some code to make things simpler):

object instance = new MyType1();

Expression expression = ... // n => n.Property1

ParameterExpression instanceParameter = Expression.Variable(
    typeof(object), "instance");
ParameterExpression resultVariable = Expression.Variable(
    typeof(MyType2), "result");

LabelTarget useDefaultLabel = Expression.Label("useDefault");
LabelTarget returnLabel = Expression.Label("return");

List<Expression> targetFragments = new List<Expression>();

MemberInfo memberInfo = (MemberInfo)expression.Body.Member;

MemberExpression member = ConstantExpression.MakeMemberAccess(
    Expression.Convert(instanceParameter, memberInfo.DeclaringType),
    memberInfo);

targetFragments.Add(
    Expression.Assign(
        resultVariable,
        Expression.Convert(member, typeof(MyType2))));

targetFragments.Add(Expression.Goto(returnLabel));
targetFragments.Add(Expression.Label(useDefaultLabel));
targetFragments.Add(Expression.Assign(resultVariable,
    Expression.Default(typeof(MyType2))));
targetFragments.Add(Expression.Label(returnLabel));

targetFragments.Add(resultVariable);

Expression finalExpression = Expression.Block(
    new[] { instanceParameter, resultVariable },
    targetFragments);

ParameterExpression parameter = Expression.Variable(typeof(object));

MyType2 result = Expression.Lambda<Func<T, MyType2>>(expression, parameter)
    .Compile()(instance);

However, the invoke throws the following exception:

Object reference not set to an instance of an object. at lambda_method(Closure , Object )

I believe this is occurring as a result of the$result = (MyType2)((MyType1)$instance).Property1; I don't see why assignment is necessary since the instance that is supplied to the expression isn'tnull .

1
1
8/1/2010 11:51:12 PM

Accepted Answer

The reality that

ParameterExpression parameter = Expression.Variable(typeof(object));

In essence, you aren't even looking at the item you pass in; you are just looking at the body, which is specified after all as the clue.instanceParameter This is just an unassigned variable in your code.

In essence, omit the conclusionparameter make a proclamation but don't make oneinstanceParameter being a variable

Expression finalExpression = Expression.Block(
    new[] { resultVariable },
    targetFragments);

MyType2 result = Expression.Lambda<Func<object, MyType2>>(
      finalExpression, instanceParameter).Compile()(instance);
3
8/2/2010 5:40:08 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow