【发布时间】:2011-09-09 21:49:42
【问题描述】:
我正在编写一个简单的映射器类来克隆和复制不同/相同类型的不同对象之间的属性。
映射是通过反射执行的,对值类型、列表和引用类型工作得很好。
一切都适用于像这样的类型:
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; }
}
其中 AnotherType 是类类型。引用类型的映射是通过递归检查属性来执行的,直到所有属性都以这种方式映射到目标:
object value = mapFrom.GetValue(input, null);
mapTo.SetValue(output, value, null);
其中 mapForm 和 mapTo 是 PropertyInfo 对象。
当具有“位图”属性的新类型出现时,问题就开始了,我意识到不能以同样的方式处理整个对象类。
Class NewType
{
public Bitmap Bitmap{get;set;}
public string Name{get;set;}
}
对于此类情况,您建议怎么做?显然复制属性不会导致原始 Bitmap 对象的新副本。
PS
我不能使用 automapper/emit mapper 或任何其他外部包。
【问题讨论】:
-
为什么不能使用例如 Automapper?
-
这是这个项目的政策