You can use serialization to make the copy pretty easily by creating a SerializationBinder. This will allow you to deserialize from one type to another.
class MySerializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
Type typeToDeserialize = null;
// To return the type, do this:
if(typeName == "TypeToConvertFrom")
{
typeToDeserialize = typeof(TypeToConvertTo);
}
return typeToDeserialize;
}
}
As long as the type properties line up you can use this to Serialize the From type and then deserialize into the To type using a BinaryFormatter and setting the Binder property to an instance of your binder class. If you have several types of objects you can use one Binder to cover all the different types.
Reflection is another option that you can look into to solve this problem. If the property names are exactly the same you could write a simple method that takes values from one property and assigns to the property of the same name.
public static void CopyObject(object source, object destination)
{
var props = source.GetType().GetProperties();
foreach (var prop in props)
{
PropertyInfo info = destination.GetType().GetProperty(prop.Name);
if (info != null)
{
info.SetValue(destination, prop.GetValue(source, null), null);
}
}
}