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 3698110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:56:30+00:00 2026-05-19T04:56:30+00:00

This is a learning exercise in expression trees. I have this working code: class

  • 0

This is a learning exercise in expression trees.

I have this working code:

class Foo
{
    public int A { get; set; }
    public string B { get; set; }
}

class Bar
{
    public int C { get; set;}
    public string D { get; set; }
}

class FieldMap
{
    public PropertyInfo From { get; set; }
    public PropertyInfo To { get; set; }

}

class Program
{
    static Action<TFrom, TTo> CreateMapper<TFrom, TTo>(IEnumerable<FieldMap> fields)
    {
        ParameterExpression fromParm = Expression.Parameter(typeof(TFrom), "from");
        ParameterExpression toParm = Expression.Parameter(typeof(TTo), "to");

        //var test = new Func<string, string>(x => x);
        //var conversionExpression = Expression.Call(null, test.Method);

        var assignments = from fm in fields
                            let fromProp = Expression.Property(fromParm, fm.From)
                            let toProp = Expression.Property(toParm, fm.To)
                            select Expression.Assign(toProp, fromProp);

        var lambda = Expression.Lambda<Action<TFrom, TTo>>(
            Expression.Block(assignments), 
            new ParameterExpression[] { fromParm, toParm });

        return lambda.Compile();
    }

    static void Main(string[] args)
    {
        var pa = typeof(Foo).GetProperty("A");
        var pb = typeof(Foo).GetProperty("B");
        var pc = typeof(Bar).GetProperty("C");
        var pd = typeof(Bar).GetProperty("D");

        var mapper = CreateMapper<Foo, Bar>(new FieldMap[] 
        { 
            new FieldMap() { From = pa, To = pc }, 
            new FieldMap() { From = pb, To = pd } 
        });

        Foo f = new Foo();
        Bar b = new Bar();

        f.A = 20;
        f.B = "jason";
        b.C = 25;
        b.D = "matt";

        mapper(f, b);     // copies properties from f into b
    }
}

Works nicely. As noted it copies the corresponding properties from f to b. Now, supposing I wanted to add some conversion or formatting method that takes the “from property”, does some magic, and then sets the “to property” equal to the result. Note the two commented out lines in the middle of CreateMapper.

How do I accomplish this? I got this far, but I’m sort of lost now.

  • 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-19T04:56:31+00:00Added an answer on May 19, 2026 at 4:56 am

    Your code sample is almost there; you can use Expression.Call to do the transformation as you are clearly trying to do. Instead of assigning toProp to the fromProp MemberExpression, you can assign to a MethodCallExpression representing the value of the transformation.

    The tricky part here is to figure out how to do the transformation, which I assume will vary for different properties.

    You can replace the LINQ expression with:

    var assignments = from fm in fields
                      let fromProp = Expression.Property(fromParm, fm.From)
                      let fromPropType = fm.From.PropertyType
                      let fromTransformed 
                           = Expression.Call(GetTransform(fromPropType), fromProp)
                      let toProp = Expression.Property(toParm, fm.To)
                      select Expression.Assign(toProp, fromTransformed);
    

    (Notice that the right-hand side of the assignment is now fromTransformed rather than fromProp.)

    where GetTransform looks something like (I’ve assumed here that the nature of the transformation depends only on the type of the property):

    private static MethodInfo GetTransform(Type type)
    {
        return typeof(Program).GetMethod(GetTransformName(type));
    }
    
    private static string GetTransformName(Type type)
    {
        if (type == typeof(int))
            return "MapInt";
    
        if (type == typeof(string))
            return "MapString";
    
        throw new ArgumentException("Unknown type");
    }
    

    Then the only thing left to do is filling in the transformations themselves; for example:

    public static int MapInt(int x)  { return x * 2; }
    
    public static string MapString(string x)  { return x + x;  }
    

    Then, your usage-test method would produce:

    b.c == 40
    b.d == "jasonjason"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

query level: beginner As part of a learning exercise I have written code that
I'm learning objective-C and Cocoa and have come across this statement: The Cocoa frameworks
I have recently started learning F#, and this is the first time I've ever
I'm still learning ASP.NET and I often see code like this throughout parts of
Im learning lisp and im pretty new at this so i was wondering... if
This is going to sound like a silly question, but I'm still learning C,
I am learning Ruby on Rails so I'm sure I'll find this out sooner
I'm learning ASP.NET MVC Framework, From some articles like this , it seems that
I'm learning about POSIX threads right now, but I guess this is just a
I was just reading through Learning Python by Mark Lutz and came across this

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.