jeudi 12 mai 2016

How can I use reflection to change properties with missing setters?

I have two classes with property getters only

public class A 
{
    public A(string name) 
    {
        Name = name;
    }
    public string Name { get; }
    public string Value { get; }
    public string Data { get; }
}

public class B
{
    public B(string name) 
    {
        Name = name;
    }
    public string Name { get; }
    public string Value { get; }
}

They are different in shape but share some of the same property names and types. How can I copy values when they only have getters?

This is a typical scenario when I send an object as a constructor parameter to extract values from in the new object. Then I need to copy values one by one. This can produce lots of code and is hard to maintain.

Can this be made simpler? Is there a way to use reflection to copy objects when the target only has getter properties?





Aucun commentaire:

Enregistrer un commentaire