15. March 2011 09:56
Today I found that the child class can't be passed over a WCF service as the base class. I tried using the KnownType and ServiceKnownType attributes, which did allow the transfer, but then EntityFramework didn't like the child class. My solution was to write a quick shallow copy function. Enjoy!
public static class Copy
{
public static T Shallow< T>(this object source)
{
return Shallow< T>(source, (T)Activator.CreateInstance(typeof(T)));
}
public static T Shallow<T>(this object source, T target)
{
foreach (PropertyInfo pi in typeof(T).GetProperties())
{
var prop = source.GetType().GetProperty(pi.Name);
if(prop != null)
prop.SetValue(target, pi.GetValue(source, null), null);
}
return target;
}
}
fbe510e4-61b9-4482-a1be-dbc6337feb77|3|5.0
By: CoderForRent
Category:
Tags: