C# 从非泛型类型到泛型类型的自动映射

C# 从非泛型类型到泛型类型的自动映射,c#,generics,mapping,automapper,mapper,C#,Generics,Mapping,Automapper,Mapper,是否可能(以及如何)创建从非泛型类型到泛型类型的映射? 假设我们有: public interface IFoo { string Foo { get; set; } } public interface IGenericFoo<TDestination> where TDestination : class { string Baboon { get; set; } } 公共接口IFoo { 字符串Foo{get;set;} } 公共接口IGenericFo

是否可能(以及如何)创建从非泛型类型到泛型类型的映射? 假设我们有:

public interface IFoo
{
     string Foo { get; set; }
}

public interface IGenericFoo<TDestination> where TDestination : class
{
     string Baboon { get; set; }
}
公共接口IFoo
{
字符串Foo{get;set;}
}
公共接口IGenericFoo,其中TDestination:class
{
字符串狒狒{get;set;}
}
我试图通过这样做()来使用开放泛型:

CreateMap(typeof(IFoo),typeof(IGenericFoo)
但在运行时失败,出现以下错误:

{“该类型或方法有1个泛型参数,但提供了0个泛型参数。必须为每个泛型参数提供一个泛型参数。”}


Automapper版本:4.2.1

这仅适用于Automapper版本5.x及更高版本。以下是一个工作示例:

using AutoMapper;
using System;

public class Program
{
    public class Source : IFoo
    {
        public string Foo { get; set; }
    }

    public class Destination<T> : IGenericFoo<T> where T : class
    {
        public string Baboon { get; set; }
    }

    public interface IFoo
    {
        string Foo { get; set; }
    }

    public interface IGenericFoo<TDestination> where TDestination : class
    {
        string Baboon { get; set; }
    }

    public static void Main()
    {
        // Create the mapping
        Mapper.Initialize(cfg => cfg.CreateMap(typeof(Source), typeof(Destination<>)));

        var source = new Source { Foo = "foo" };

        var dest = Mapper.Map<Source, Destination<object>>(source);

        Console.WriteLine(dest.Baboon);
    }
}
使用AutoMapper;
使用制度;
公共课程
{
公共类来源:IFoo
{
公共字符串Foo{get;set;}
}
公共类目的地:IGenericFoo其中T:class
{
公共字符串Baboon{get;set;}
}
公共接口IFoo
{
字符串Foo{get;set;}
}
公共接口IGenericFoo,其中TDestination:class
{
字符串狒狒{get;set;}
}
公共静态void Main()
{
//创建映射
初始化(cfg=>cfg.CreateMap(typeof(Source),typeof(Destination));
var source=新源{Foo=“Foo”};
var dest=Mapper.Map(源);
控制台写入线(目标狒狒);
}
}

这仅适用于AutoMapper 5.x及更高版本。以下是一个工作示例:

using AutoMapper;
using System;

public class Program
{
    public class Source : IFoo
    {
        public string Foo { get; set; }
    }

    public class Destination<T> : IGenericFoo<T> where T : class
    {
        public string Baboon { get; set; }
    }

    public interface IFoo
    {
        string Foo { get; set; }
    }

    public interface IGenericFoo<TDestination> where TDestination : class
    {
        string Baboon { get; set; }
    }

    public static void Main()
    {
        // Create the mapping
        Mapper.Initialize(cfg => cfg.CreateMap(typeof(Source), typeof(Destination<>)));

        var source = new Source { Foo = "foo" };

        var dest = Mapper.Map<Source, Destination<object>>(source);

        Console.WriteLine(dest.Baboon);
    }
}
使用AutoMapper;
使用制度;
公共课程
{
公共类来源:IFoo
{
公共字符串Foo{get;set;}
}
公共类目的地:IGenericFoo其中T:class
{
公共字符串Baboon{get;set;}
}
公共接口IFoo
{
字符串Foo{get;set;}
}
公共接口IGenericFoo,其中TDestination:class
{
字符串狒狒{get;set;}
}
公共静态void Main()
{
//创建映射
初始化(cfg=>cfg.CreateMap(typeof(Source),typeof(Destination));
var source=新源{Foo=“Foo”};
var dest=Mapper.Map(源);
控制台写入线(目标狒狒);
}
}

当您应用映射时,您希望
TDestination
是什么?实际上,我将在Mapper.Map invoke期间提供任何类。当您应用映射时,您希望
TDestination
是什么?实际上,我将在Mapper.Map invoke期间提供任何类