在客户端上自动生成代码后,缺少类型映射配置或不支持的映射
本文关键字:映射 类型 配置 不支持 客户端 自动生成 代码 | 更新日期: 2023-09-27 18:25:11
我有一个简单的类:
[DataContract]
public class ClaimView
{
[DataMember]
public string Type { get; set; }
[DataMember]
public string Value { get; set; }
[DataMember]
public string ValueType { get; set; }
}
我在客户端通过wcf服务器引用接收此类,并尝试将其映射到System.Security.Claims.Claim,如下所示:
Mapper.CreateMap<ClaimView, Claim>()
.ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.Type))
.ForMember(dest => dest.Value, opt => opt.MapFrom(src=> src.Value))
.ForMember(dest => dest.ValueType, opt => opt.MapFrom(src => src.ValueType))
.ForMember(dest => dest.Issuer, opt => opt.Ignore())
.ForMember(dest => dest.OriginalIssuer, opt => opt.Ignore())
.ForMember(dest => dest.Properties, opt => opt.Ignore())
.ForMember(dest => dest.Subject, opt => opt.Ignore());
在我从autommaper那里得到错误后,他无法将其转换。缺少什么或出了什么问题?
因为Claim没有默认构造函数,所以您需要告诉Automapper要使用哪个构造函数,例如:
Mapper.CreateMap<ClaimView, Claim>()
.ConstructUsing(cv => new Claim(cv.Type, cv.Value, cv.ValueType))
.ForMember(dest => dest.Type, opt => opt.MapFrom(src => src.Type))
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.Value))
.ForMember(dest => dest.ValueType, opt => opt.MapFrom(src => src.ValueType))
.ForMember(dest => dest.Issuer, opt => opt.Ignore())
.ForMember(dest => dest.OriginalIssuer, opt => opt.Ignore())
.ForMember(dest => dest.Properties, opt => opt.Ignore())
.ForMember(dest => dest.Subject, opt => opt.Ignore());
然后此测试将通过:
public void Test()
{
ClaimIdentityView civ = new ClaimIdentityView
{
ClaimViewList = new List<ClaimView>
{
new ClaimView
{
Type = "type",
Value = "val",
ValueType = "string"
}
}
};
var claims = civ.ClaimViewList.Select(Mapper.Map<ClaimView, Claim>).ToList();
Assert.AreEqual(1, claims.Count);
Assert.AreEqual("type", claims.Single().Type);
Assert.AreEqual("val", claims.Single().Value);
Assert.AreEqual("string", claims.Single().ValueType);
}