Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.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#_Automapper - Fatal编程技术网

C# 将接口映射到具体类

C# 将接口映射到具体类,c#,automapper,C#,Automapper,我有一种汽车制造商的奇怪行为。如果我有一个实现两个接口的类,它们都在CreateMap中注册,那么只有一个被映射,我不明白为什么。看起来AutoMapper只映射了在接口列表中第一个提到的接口 有什么想法吗?请检查以下内容以了解问题所在 来自fiddler的代码 我要映射到的DTO: class Dto { public string StringValue {get;set;} public int Value {get;set;} } interface ISourceA

我有一种汽车制造商的奇怪行为。如果我有一个实现两个接口的类,它们都在
CreateMap
中注册,那么只有一个被映射,我不明白为什么。看起来AutoMapper只映射了在接口列表中第一个提到的接口

有什么想法吗?请检查以下内容以了解问题所在

来自fiddler的代码

我要映射到的DTO:

class Dto {
    public string StringValue {get;set;}
    public int Value {get;set;}
}
interface ISourceA {
    int Value {get;}
}

interface ISourceB {
    string StringValue {get;}
}
接口-我想映射的源:

class Dto {
    public string StringValue {get;set;}
    public int Value {get;set;}
}
interface ISourceA {
    int Value {get;}
}

interface ISourceB {
    string StringValue {get;}
}
…和两个实现。一个有效,另一个无效:/

class MultiSource: ISourceA, ISourceB {
    private readonly string _s;
    private readonly int _v;

    public MultiSource(int v, string s) {
        _v = v;
        _s = s;
    }

    int ISourceA.Value { get { return _v; }}
    string ISourceB.StringValue { get { return _s; }}
}

class StringSource: ISourceB {
    public StringSource(string value) {
        StringValue = value;
    }

    public string StringValue {get; private set;}
}
以下是我如何使用它:

public class Program
{
    public static void Main()
    {
        Mapper.CreateMap<ISourceA, Dto>();
        Mapper.CreateMap<ISourceB, Dto>();

        var ms = new MultiSource(234, "woefjweofij");
        var ss = new StringSource("iuahergiuw");

        // This one is fine
        Console.WriteLine(JsonConvert.SerializeObject(Mapper.Map<Dto>((ISourceA)ms)));
        // This one is the same as that above. This is not what I intended to have in return :/ 
        Console.WriteLine(JsonConvert.SerializeObject(Mapper.Map<Dto>((ISourceB)ms)));
        // This works as expected
        Console.WriteLine(JsonConvert.SerializeObject(Mapper.Map<Dto>(ss)));
    }
}
公共类程序
{
公共静态void Main()
{
CreateMap();
CreateMap();
var ms=新的多源(234,“woefjweofij”);
var ss=新的StringSource(“iuahergiuw”);
//这个很好
WriteLine(JsonConvert.SerializeObject(Mapper.Map((isoourcea)ms));
//这个和上面的一样。这不是我想要的回报:/
WriteLine(JsonConvert.SerializeObject(Mapper.Map((ISourceB)ms));
//这是意料之中的事
WriteLine(JsonConvert.SerializeObject(Mapper.Map(ss));
}
}

你能从这里的小提琴上复制一下代码吗,让回答的人更容易看到它?哪个类不识别接口?@dennisanberlin the
MultiSource
。如果将其用作后一个接口,则它与automapper不兼容:/n您可以通过运行程序来查看它。第二个映射与第一个映射完全相同,这不是我想要的不可能: