public static class CopyUtility {
public enum TDCType {
Customer,
Login
}
public static void CopyObject(object source, out object tdcDestination, TDCType type)
{
tdcDestination = type == TDCType.Customer ? new TDCCustomerClass() : null;
TDCPropertyAttribute tdcAttrib;
//foreach property in source
foreach (PropertyInfo sourcePropertyInfo in source.GetType().GetProperties()) {
//is there a getter
if (sourcePropertyInfo.CanRead) {
//foreach custom attribute
foreach (Attribute attr in sourcePropertyInfo.GetCustomAttributes(true)) {
//try and cast to aatrib
tdcAttrib = attr as TDCPropertyAttribute;
//did we have a TDCPropertyAttribute?
if (null != tdcAttrib) {
PropertyInfo destinationProperty = tdcDestination.GetType().GetProperty(tdcAttrib.PropertyName);
if (destinationProperty.CanWrite) {
object value = sourcePropertyInfo.GetValue(source, null);
destinationProperty.SetValue(tdcDestination, value, null);
}
Console.WriteLine("PropertyName = {0}", tdcAttrib.PropertyName);
}
}
}
}
}
}