C# 与通用目的地的映射不';不行?
我试图构建映射配置文件的层次结构,以简化为适合此层次结构的新对象创建映射的过程。但是我很难让一些映射正常工作。我使用泛型类型参数添加的映射在目标类型时似乎不起作用,但在源类型时起作用 考虑以下类型:C# 与通用目的地的映射不';不行?,c#,generics,automapper,C#,Generics,Automapper,我试图构建映射配置文件的层次结构,以简化为适合此层次结构的新对象创建映射的过程。但是我很难让一些映射正常工作。我使用泛型类型参数添加的映射在目标类型时似乎不起作用,但在源类型时起作用 考虑以下类型: public interface IRequest { string Input { get; } } public interface IResponse { string Output { get; } } public class SomeObject { public
public interface IRequest
{
string Input { get; }
}
public interface IResponse
{
string Output { get; }
}
public class SomeObject
{
public string Value { get; set; }
}
abstract class BaseProfile<TSource, TDestination> : Profile
where TSource : IRequest
where TDestination : IResponse
{
protected BaseProfile()
{
CreateMap<TSource, SomeObject>()
.ForMember(src => src.Value, opt => opt.MapFrom(dest => dest.Input));
CreateMap<SomeObject, TDestination>()
.ForMember(src => src.Output, opt => opt.MapFrom(dest => dest.Value));
}
}
将setter添加到
i响应
的输出
属性中
public interface IResponse
{
string Output { get; set; }
}
它正在尝试映射到iResponse
,无法设置属性,因为它不可写
也许这不是您想要的,因为您不希望i响应
具有可写属性。但这就是为什么它没有被设定
在这种情况下,我能确定的唯一解决方法是创建一个基本响应,该响应实现I响应
,并具有可写属性
class ResponseBase : IResponse
{
public string Output { get; set; }
}
class FooResponse : ResponseBase
{
}
然后将泛型约束更改为基类而不是接口:
abstract class BaseProfile<TSource, TDestination> : Profile
where TSource : IRequest
where TDestination : ResponseBase
抽象类BaseProfile:Profile
来源:IRequest
where TDestination:ResponseBase
该泛型约束将决定如何强制转换目标类型,进而决定属性是否可写。我尝试了许多方法,但没有机会,但这种重载只适用于私有契约设置程序
abstract class BaseProfile<TSource, TDestination> : Profile
where TSource : IRequest
where TDestination : IResponse
{
protected BaseProfile()
{
CreateMap<TSource, SomeObject>()
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.Input));
CreateMap(typeof(SomeObject), typeof(TDestination))
.ForMember("Output", opt => opt.MapFrom("Value"));
}
}
抽象类BaseProfile:Profile
来源:IRequest
地点t目的地:i响应
{
受保护的BaseProfile()
{
CreateMap()
.ForMember(dest=>dest.Value,opt=>opt.MapFrom(src=>src.Input));
CreateMap(typeof(SomeObject)、typeof(TDestination))
.ForMember(“输出”,opt=>opt.MapFrom(“值”);
}
}
也许jimmy不止一种方式使用结果哦,很简单。好的,谢谢!我当然更愿意将属性保留为只读,所以希望有一个不同的选项。它可以处理过载!!!也许jimmy可以通过多种方式使用结果
CreateMap(typeof(SomeObject),typeof(tdestation)).ForMember(“Output”,opt=>opt.MapFrom(“Value”)代码>嗯,你是对的,不过唯一需要更改的是ForMember()调用的第一个参数。因此,我们不是绑定到i响应.Output
成员,而是绑定到Output
名称。把它作为一个答案,这将是一个很好的解决办法。
public interface IResponse
{
string Output { get; set; }
}
class ResponseBase : IResponse
{
public string Output { get; set; }
}
class FooResponse : ResponseBase
{
}
abstract class BaseProfile<TSource, TDestination> : Profile
where TSource : IRequest
where TDestination : ResponseBase
abstract class BaseProfile<TSource, TDestination> : Profile
where TSource : IRequest
where TDestination : IResponse
{
protected BaseProfile()
{
CreateMap<TSource, SomeObject>()
.ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.Input));
CreateMap(typeof(SomeObject), typeof(TDestination))
.ForMember("Output", opt => opt.MapFrom("Value"));
}
}