0
我新的AutoMapperAutoMapper與領域模型
我有以下的域模型。
public class Contact
{
public string University { get; set; }
public string GraduationYear { get; set; }
}
我不想更改域模型,並且想爲GraduationYear屬性添加額外的屬性。
我已經創建了我的MVC 4項目
[System.AttributeUsage(System.AttributeTargets.Property)]
public class Mapping : System.Attribute
{
public string Name { get; set; }
}
上面使用的類屬性命名爲「ContactMapping」創建
public class ContactMapping
{
[Telephony.Helper.Attribute.Mapping(Name = "Graduate")]
public string GraduationYear { get; set; }
}
我從沒有域名服務獲取聯繫人對象屬性類有自定義屬性'映射'。
var contact = new ContactService().Get(contactPredicate).Single();
我現在做自動映射
AutoMapper.Mapper.CreateMap<Telephony.Helper.Mapping.ContactMapping, Aurora.CustomersMvc.Domain.Contact>();
var test = AutoMapper.Mapper.Map<Aurora.CustomersMvc.Domain.Contact>(contact);
var contactwithCustomAttr = contact.GetType().GetProperties().Where(p => ((Telephony.Helper.Attribute.Mapping[])p.GetCustomAttributes(typeof(Telephony.Helper.Attribute.Mapping), false))
.Any(attr => attr.ToString() == "Graduate"));
我越來越contactwithCustomAttr.Count()爲零。我希望返回GraduationYear屬性,因爲映射器應該應用我的自定義屬性。
contactwithCustomAttr返回NULL – user602291
剛纔編輯的答案。 – rivarolle
仍然返回null。只有單一屬性的測試變量。 – user602291