I want to map all properties that match between two class instances.
public class Foo
{
public bool A { get: set: }
public bool B { get: set: }
public bool C { get: set: }
public bool D { get: set: }
public bool E { get: set: }
}
public class Bar
{
public bool A { get: set: }
public bool B { get: set: }
}
Bar bar = new Bar();
bar.A = true;
bar.B = true;
How do I map the values from the bar instance to a new instance of foo (setting the properties “A” and “B” to true)? I tried to make a method for it, but i get the exception Property set method not found..
public static void MapObjectPropertyValues(object e1, object e2)
{
foreach (var p in e1.GetType().GetProperties())
{
if (e2.GetType().GetProperty(p.Name) != null)
{
p.SetValue(e1, e2.GetType().GetProperty(p.Name).GetValue(e2, null), null);
}
}
}
Any help is much appreciated, thanks!
I’ve implemented something similar to this before by abstracting the common properties into interfaces and then having a utility class that copied interface properties. What you end up with is something like this:
Just be sure to check the property can be read and written to before calling SetValue!
In the case you cannot abstract into interfaces simply checking CanRead on source and CanWrite on the destination (also checking for existance of the property on the destination) should resolve the issue you have above.
Best regards,