我有兩個具有確切屬性的對象(類)。說:映射兩個相同的對象
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問題,所以有人可以幫助,而不是試圖重塑它已經做了什麼? –