Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.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# 映射容器<;T>;使用AutoMapper进行测试_C#_Automapper - Fatal编程技术网

C# 映射容器<;T>;使用AutoMapper进行测试

C# 映射容器<;T>;使用AutoMapper进行测试,c#,automapper,C#,Automapper,我的问题与所问的问题非常相似,但我无法根据提供的答案找到解决方案。我一定是错过了即将发生的事情 我有一个自定义转换器,允许我执行以下操作: cfg.CreateMap<Container<int>, int>().ConvertUsing(new ContainerConverter<Container<int>, int>()); 不必做: cfg.CreateMap<Container<int>, int>().Con

我的问题与所问的问题非常相似,但我无法根据提供的答案找到解决方案。我一定是错过了即将发生的事情

我有一个自定义转换器,允许我执行以下操作:

cfg.CreateMap<Container<int>, int>().ConvertUsing(new ContainerConverter<Container<int>, int>());
不必做:

cfg.CreateMap<Container<int>, int>().ConvertUsing(new ContainerConverter<Container<int>, int>());
cfg.CreateMap<Container<long>, long>().ConvertUsing(new ContainerConverter<Container<long>, long>());
...

提前谢谢。

如果您不介意装箱,可以使用通用转换器

static void Main(string[] args)
{
    var config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap(typeof(Container<>), typeof(object)).ConvertUsing(typeof(ContainerConverter<>));
    });
    config.AssertConfigurationIsValid();
    //config.BuildExecutionPlan(typeof(Destination), typeof(Source)).ToReadableString().Dump();
    var mapper = config.CreateMapper();
    mapper.Map<int>(new Container<int>{ Value = 12 }).Dump();
}
public class ContainerConverter<T> : ITypeConverter<Container<T>, object>
{
    public object Convert(Container<T> source, object destination, ResolutionContext c)
    {
        return source.Value;
    }
}
public class Container<T>
{
    public T Value { get; set; }
}
static void Main(字符串[]args)
{
var config=new-MapperConfiguration(cfg=>
{
CreateMap(typeof(Container),typeof(object)).ConvertUsing(typeof(ContainerConverter));
});
config.assertconfigurationsvalid();
//config.BuildExecutionPlan(typeof(Destination),typeof(Source)).ToReadableString().Dump();
var mapper=config.CreateMapper();
Map(新容器{Value=12}).Dump();
}
公共类容器转换器:ITypeConverter
{
公共对象转换(容器源、对象目标、ResolutionContext c)
{
返回source.Value;
}
}
公营货柜
{
公共T值{get;set;}
}

@jalsh我会尝试一下解决方案。谢谢。@Lucian Bargaoanu嗨,Lucian,你能解释一下我如何系统地指定目标类型吗?你可以使用object。如果您不想这样做,可以使用@LucianBargaoanu我有一个自定义转换器,声明为:
公共类ContainerConverter:ITypeConverter
。在映射器配置中,我有:
MapperConfiguration config=newmapperconfiguration(cfg=>{…cfg.CreateMap(typeof(Container),typeof(object)).ConvertUsing(newcontainerconverter();)}
。对我来说,如果容器是一个类,但我的容器是一个结构,那么这是可行的。新的MapperConfiguration()调用引发System.ArgumentException,表示容器不能用于object类型的参数。有办法解决这个问题吗?
B dest = mapper.Map<A, B>(source);
static void Main(string[] args)
{
    var config = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap(typeof(Container<>), typeof(object)).ConvertUsing(typeof(ContainerConverter<>));
    });
    config.AssertConfigurationIsValid();
    //config.BuildExecutionPlan(typeof(Destination), typeof(Source)).ToReadableString().Dump();
    var mapper = config.CreateMapper();
    mapper.Map<int>(new Container<int>{ Value = 12 }).Dump();
}
public class ContainerConverter<T> : ITypeConverter<Container<T>, object>
{
    public object Convert(Container<T> source, object destination, ResolutionContext c)
    {
        return source.Value;
    }
}
public class Container<T>
{
    public T Value { get; set; }
}