Reputation: 35213
What i currently use
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);
}
}
}
I want to pass a third parameter, a generic list of types that i want to exclude from the mapping. For example strings and booleans. And check if p
is of a type in the list. Any help is appreciated, thanks!
Upvotes: 0
Views: 608
Reputation: 8699
You could use the p.PropertyType
property to exclude assignment if the types match exactly.
public static void MapObjectPropertyValues(object e1,
object e2,
IEnumerable<Type> excludedTypes)
{
foreach (var p in e1.GetType().GetProperties())
{
if (e2.GetType().GetProperty(p.Name) != null &&
// next line added
!(excludedTypes.Contains(p.PropertyType)))
{
p.SetValue(e1, e2.GetType().GetProperty(p.Name).GetValue(e2, null), null);
}
}
}
Upvotes: 1