我有两个具有确切属性的对象(类)。说:映射两个相同的对象
class class1
{
public string prop1{get;set;}
public string prop2{get;set;}
}
class class2
{
public string prop1{get;set;}
public string prop2{get;set;}
}
我想使用反射这些类映射,我已经使用AutoMapper,它不适合我的情况下工作,因为我有内部目标对象....
当使用反射我需要通过属性名,我不希望这样做一一有其他的方式:
PropertyInfo propinfo = listToReturn.GetType().GetProperty(nameofproperty);
编辑::
这是我曾尝试与automapper:
internal static DTO_objectclass ConvertFOS(objectclass q)
{
DTO_objectclass resultsToReturn = new DTO_objectclass();
AutoMapper.Mapper.CreateMap<objectclass , DTO_objectclass >();
resultsToReturn = AutoMapper.Mapper.Map<objectclass , DTO_objectclass>(q);
return resultsToReturn;
}
这个工程直到它在那里我有对象类是这样一个特性:
property class3 parentClass{get; set;}
和DTO_objectclass我得到:
property guid parentClass{get; set;}
从哪里获得的转换失败例外..
Trying to map System.Guid to parentclient.\nUsing mapping configuration for DTO_objectclass to objectclass \nDestination property: ParentClass\nException of type 'AutoMapper.AutoMapperMappingException' was thrown.
你为什么要这样做,那是浪费代码。 –
不这样做,它是过早的优化,使用反射会降低你的性能 –
为什么不发布你的Automapper问题,所以有人可以帮助,而不是试图重塑它已经做了什么? –