Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 与通用目的地的映射不';不行?_C#_Generics_Automapper - Fatal编程技术网

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"));
        }
    }