1

I'm building a mapping engine.

The "path" will be coming in as a string, it is shown here as mappingAddress. The value in theAddressTypeValue is what needs to be populated the already instantiated object 2 levels deep in employee.

How do I update "TheAddressType" in the 2 levels deep of the "employee"

Thanks!

class Program
{
    static void Main(string[] args)
    {
        string mappingAddress = "EmployeeAddress.ChildAddressType.TheAddressType";

        string theAddressTypeValue = "A Home";

        Employee employee = new Employee();

        //Magic code here
    }
}

public class Employee
{
    public Employee()
    {
        EmployeeAddress = new Address();
    }

    public Address EmployeeAddress { get; set; }
}

public class Address
{
    public Address()
    {
        ChildAddressType = new AddressType();
    }

    public AddressType ChildAddressType { get; set; }
}

public class AddressType
{
    public string TheAdddressType { get; set; }
}
Matt H
  • 138
  • 1
  • 9
  • You could use reflection. But why it comes as a string in first place? – Sriram Sakthivel Sep 12 '14 at 15:34
  • 3
    You should really, really avoid trying to execute strings as code. It's extremely insecure, error prone, inefficient, and just all around no fun for everyone involved. – Servy Sep 12 '14 at 15:34
  • "I'm building a mapping engine." : have you heard of AutoMapper ? (http://automapper.org/) – aybe Sep 12 '14 at 15:40
  • Automapper will not work in our situation here (or at least my knowledge of it will not apply). I have no control over the way the path is brought in, that's the way the data (stored in a table). – Matt H Sep 12 '14 at 15:45

1 Answers1

2

Well, if there's really can't change any of the existing stuff.....

I believe this is what you're looking for:

How to set Vaues to the Nested Property using C# Reflection.?

Your program should look something like this:

class Program
{
    static void Main(string[] args)
    {
        string mappingAddress = "EmployeeAddress.ChildAddressType.TheAddressType";

        string theAddressTypeValue = "A Home";

        Employee employee = new Employee();

        //Magic code - Thanks Jon Skeet
        SetProperty(mappingAddress, employee, theAddressTypeValue);
    }

    public static void SetProperty(string compoundProperty, object target, object value)
    {
        string[] bits = compoundProperty.Split('.');
        for (int i = 0; i < bits.Length - 1; i++)
        {
            PropertyInfo propertyToGet = target.GetType().GetProperty(bits[i]);
            target = propertyToGet.GetValue(target, null);
        }
        PropertyInfo propertyToSet = target.GetType().GetProperty(bits.Last());
        propertyToSet.SetValue(target, value, null);
    }
}

public class Employee
{
    public Employee()
    {
        EmployeeAddress = new Address();
    }

    public Address EmployeeAddress { get; set; }
}

public class Address
{
    public Address()
    {
        ChildAddressType = new AddressType();
    }

    public AddressType ChildAddressType { get; set; }
}

public class AddressType
{
    public string TheAddressType { get; set; }
}
Community
  • 1
  • 1
Kaz
  • 691
  • 3
  • 14