开发者

Automapper -cannot resolve the Generic List

开发者 https://www.devze.com 2022-12-25 00:22 出处:网络
Mapper.CreateMap<BusinessObject, Proxy.DataContacts.DCObject>() .ForMember(x => x.Ext开发者_Go百科ensionData, y => y.Ignore())
 Mapper.CreateMap<BusinessObject, Proxy.DataContacts.DCObject>()
 .ForMember(x => x.Ext开发者_Go百科ensionData, y => y.Ignore())
 .ForMember(z => z.ValidPlaces, a=> a.ResolveUsing(typeof(ValidPlaces)));
 Mapper.AssertConfigurationIsValid();  

 proxydcObject = Mapper.Map<BusinessObject, Proxy.DataContracts.DCObject>(_instanceOfBusinessObject); //throws an exception saying ValidPlaces could not be resolved

 public class BusinessObject
 {
     public Enum1 Enum1 { get; set; }
     public List<ValidPlaces> ValidPlaces{ get; set; }
 }

 public class ValidPlaces
 {
     public int No { get; set; }
     public string Name { get; set; }
}

public class DCObject
{
    [DataMember]
    public Enum1 Enum1 { get; set; }
    [DataMember]
    public List<ValidPlaces> ValidPlaces{ get; set; }
}

Mapper.CreateMap works find when Mapper.AssertConfigurationIsValid(); (No exceptions thrown on this line) is called but when I actually call into the WCF service on the next line which is not shown here the Automapper throws and exception saying ValidPlaces could not be mapped.Works fine if I put Ignore() on the ValidPlaces property but ideally want that passed.

Any AutoMapper experts out there pls help.


You should be able to ditch the line for ValidPlaces:

Mapper.CreateMap<BusinessObject, Proxy.DataContacts.DCObject>()
 .ForMember(x => x.ExtensionData, y => y.Ignore());

Value resolvers are for a custom class to do the value resolution, and must be of type IValueResolver. That's some defensive coding I should put in place. But for List of T -> List of U, as long as AutoMapper can map T -> U, it will work. In your situation, since T == U, then you don't have to do anything extra. Otherwise, you have to map T to U (but not List of T -> List of U).

0

精彩评论

暂无评论...
验证码 换一张
取 消