ExpressionTree-Property

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

public class TestClass
{
    public int myProperty {get; set;}
}

public class Program
{
    public static void Main(string[] args)
    {
		TestClass obj = new TestClass();
		obj.myProperty = 60;
		
        Console.WriteLine("****************** C# Equivalent Output **************************\n\n");

        Console.WriteLine(obj.myProperty);

        Console.WriteLine("\n\n***************** Expression Tree Output **************************\n\n");

        Expression propertyExpr = Expression.Property(
            Expression.Constant(obj),
            "myProperty"
        );

        Console.WriteLine(Expression.Lambda<Func<int>>(propertyExpr).Compile()());
    }
}
;