What C#/Linq code copies all matching property name values between two objects without knowing their type?
I have a well known POCO class of Customer to return from my method. However, I only populate properties specified by an ever changing Expression 开发者_运维知识库p => new {p.id, p.name} for example, as a parameter to the method.
Somehow I need to copy all matching fields between these two objects.
var returnObject = IList<Customer>();
var partialFieldObject = DC.Customers.Select( expParameter); // wont know the fields
foreach( var partialRecord in partialFieldObject)
{ foreach (var property in partialRecord // Pseudo code)
{
returnObject[property] = property.value; // More Pseudo code
}
}
End result is a strongly typed Customer POCO returned that only has the selected fields populated with values.
Some simple reflection does the trick, assuming the properties on partialFieldObject
line up exactly (case-sensitive) with properties on Customer
...
void SetProperties(object source, object target)
{
var customerType = target.GetType();
foreach (var prop in source.GetType().GetProperties())
{
var propGetter = prop.GetGetMethod();
var propSetter = customerType.GetProperty(prop.Name).GetSetMethod();
var valueToSet = propGetter.Invoke(source, null);
propSetter.Invoke(target, new[] { valueToSet });
}
}
You can use AutoMapper - it's built to do this stuff I think
Adapting dahlbyk's accepted answer, I've ended up using the following in a constructor that accepted a base-type and needed to mirror the contents:
var thisType = this.GetType();
foreach (var prop in baseObj.GetType().GetProperties()
.Where(p => thisType.GetProperty(p.Name) != null))
{
var propGetter = prop.GetGetMethod();
var propSetter = thisType.GetProperty(prop.Name).GetSetMethod();
if (propSetter != null)
propSetter.Invoke(this, new[] { propGetter.Invoke(baseObj, null) });
}
Basically, step through each property in the baseObj
type where this
type has a property with a matching name, and locate the get
method on the baseObj
type as well as the set
method for the matching named property on this
type and if there is a set
method available on this
, invoke it using the method returned from the invocation of the get
method on the baseObj
type.
Works well for public & private properties of any (matching) type on both types.
Note: You'll need a reference to System.Linq
for the .Where()
func in the loop
Not that different, just the most compact and cleanest form I found:
void CopyProperties(object src, object dest) {
foreach (var source in src.GetType().GetProperties().Where(p => p.CanRead)) {
var prop = dest.GetType().GetProperty(source.Name);
if (prop?.CanWrite == true)
prop.SetValue(dest, source.GetValue(src, null), null);
}
}
public static void CopyPropertiesTo<T, TU>(this T source, TU dest)
{
var sourceProps = typeof (T).GetProperties().Where(x => x.CanRead).ToList();
var destProps = typeof(TU).GetProperties()
.Where(x => x.CanWrite)
.ToList();
foreach (var sourceProp in sourceProps)
{
if (destProps.Any(x => x.Name == sourceProp.Name))
{
var p = destProps.First(x => x.Name == sourceProp.Name);
p.SetValue(dest, sourceProp.GetValue(source, null), null);
}
}
}
void Copy(object copyToObject, object copyFromObject)
{
BindingFlags flags = BindingFlags.Public | BindingFlags.Instance;
FieldInfo[] fields = copyFromObject.GetType().GetFields(flags);
for (int i = 0; i < fields.Length; ++i)
{
FieldInfo fromField = copyFromObject.GetType().GetField(fields[i].Name, flags);
FieldInfo toField = copyToObject.GetType().GetField(fields[i].Name, flags);
if(fromField != null)
{
toField.SetValue(copyToObject, fromField.GetValue(copyFromObject));
}
}
}
精彩评论