Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 968311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:27:30+00:00 2026-05-16T02:27:30+00:00

I have a tree expression that looks like this: .Block( System.Object $instance, MyType2 $result)

  • 0

I have a tree expression that looks like this:

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

These are the custom types that are used in the expression tree:

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

public class MyType2
{
}

Finally, this is how I build, compile and invoke the expression tree (this won’t run exactly like this because I’ve left out some code to simplify things):

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

The invoke throws the following exception however:

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

I think this is happening because of the $result = (MyType2)((MyType1)$instance).Property1; assignment but I don’t understand why because the instance that is passed to the expression isn’t null.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:27:31+00:00Added an answer on May 16, 2026 at 2:27 am

    The fact that:

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

    Is defined after all the body should be the clue; essentially, you simply aren’t even looking at the object you pass in; you are only looking at instanceParameter, which is (in your code) simply an unassigned variable.

    Basically, drop the final parameter declaration, and don’t declare instanceParameter as a variable:

    Expression finalExpression = Expression.Block(
        new[] { resultVariable },
        targetFragments);
    
    MyType2 result = Expression.Lambda<Func<object, MyType2>>(
          finalExpression, instanceParameter).Compile()(instance);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It seems to me there are two ways: 1- wait… May 16, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer CVS is really a step back, but ok ;) If… May 16, 2026 at 6:37 pm
  • Editorial Team
    Editorial Team added an answer Because the constraint is discoverable without having to execute the… May 16, 2026 at 6:37 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have two expression trees defined like this: private Expression<Func<TEntity, TPropertyResult>> PropertyAccessor { get;
I have a binary tree based mathematical expression parser I built, which works great
I have an F# quotation that I manipulate (I add object pools everywhere to
This may be familiar to some. I have a wrapper class Ex that wraps
I'm creating a Yahoo! Widget and have done this before without any problems (creating
I'm looking for possible solutions for the following scenario: I have a service that
Hey, I wrote this (fortran) with the aim of finding the minimum spanning tree
I have a massive XML file. However, I'm only interested in a single small
In C#, there's the standard initializer technique { Property1 = a, Property2 = b
I'm very new to Silverlight. I've downloaded VS 2010 so that I can mess

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.