I am writing a simple mapper class to clone and copy properties between different objects of different/same types.
Mapping is performed via reflection and works just fine for value types, lists and reference types.
Everything works for types like this one:
class SimpleValueTypes
{
public string Name { get; set; }
public int Number { get; set; }
public long LongNumber { get; set; }
public float FloatNumber { get; set; }
public bool BooleanValue { get; set; }
public AnotherType AnotherProperty{ get; set; }
}
where AnotherType is a class type. Mapping of reference types is performed by inspecting the properties recursively until all the properties are mapped to the destination pretty much this way:
object value = mapFrom.GetValue(input, null);
mapTo.SetValue(output, value, null);
where mapForm and mapTo are PropertyInfo objects.
Problems began when a new type with a “Bitmap” property arrived and I realised a whole class of objects cannot be treated the same way.
Class NewType
{
public Bitmap Bitmap{get;set;}
public string Name{get;set;}
}
What would you reccomend to do for cases like these? Obviously copying the properties would not lead to a new copy of the original Bitmap object.
PS
I can’t use automapper/emit mapper or any other external packages.
If you are implementing a mapper and not use some existing ones :
I think that this always worked on your side, it’s only a case of your project (which is a good news). The thing is that, by me, you arrived on point where you need to implement custom mapper. So define some base class like
BaseMapperwith mapping basic functionality, and derive from itBitmapMapper:BaseMapperclass which takes care of mapping ofBitmaptype objects.After a while could be you will get some other type, you will create
MyTypeMapper : BaseMapperwith concrete implementation of mapping.This just a basic idea, you should think of this to find out the most suitable way of concrete implementation of it for your project.
If using your own mapper is not mandatory for you, you can use some already existing solutions available on market, like for example Automapper
Regards.