用于.Net的AutoMapper允许您从一种类型映射到另一种类型。它最基本的功能是通过复制类型B中存在的类型A的属性值(具有匹配的名称和类型),从另一种类型的类创建另一种类型的类。
示例:
public class ClassA {
public string StringProp { get; set; }
public int IntProp { get;set; }
}
public class ClassB {
public string StringProp { get; set; }
public int SomeIntProp { get; set; }
}
ClassA classAInstance = new ClassA { StringProp = "Test", IntProp = 5 };
ClassB classBInstance = Mapper.Map<ClassA, ClassB>(classAInstance);
// This creates a new instance of ClassB and sets its StringProp property to "Test".
// It does not set the property on ClassB called "SomeIntProp" because there is no
// property on ClassA called "SomeIntProp"
Objective-C有类似的东西吗?
发布于 2010-10-21 05:12:42
如果你真的真的想这样做,你可以使用键值编码,但我会强烈地考虑为什么你一开始就想要做这样的事情。
要使用键值编码来执行此操作,请使用-dictionaryWithValuesForKeys:
和-setValuesForKeysWithDictionary:
。它们被记录在NSKeyValueCoding Protocol Reference中。
发布于 2013-04-22 23:50:27
发布于 2014-06-05 08:00:06
在使用AutoMapper之后,我在.NET世界遇到了类似的问题,最终我使用了OCMapper库。
功能:
不需要对模型进行子类化或添加任何额外代码
https://stackoverflow.com/questions/3983037
复制