4

根据AutoMapper 文档,我应该能够使用以下方法创建和使用自定义类型转换器的实例:

var dest = Mapper.Map<Source, Destination>(new Source { Value = 15 },
    opt => opt.ConstructServicesUsing(childContainer.GetInstance));

我有以下源和目标类型:

public class Source {
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }
}

public class Destination {
    public int Value1 { get; set; }
    public DateTime Value2 { get; set; }
    public Type Value3 { get; set; }
}

以及以下类型转换器:

public class DateTimeTypeConverter : ITypeConverter<string, DateTime> {
    public DateTime Convert(ResolutionContext context) {
        return System.Convert.ToDateTime(context.SourceValue);
    }
}

public class SourceDestinationTypeConverter : ITypeConverter<Source, Destination> {
    public Destination Convert(ResolutionContext context) {
        var dest = new Destination();
        // do some conversion
        return dest;
    }
}

这个简单的测试应该断言日期属性之一得到正确转换:

[TestFixture]
public class CustomTypeConverterTest {
    [Test]
    public void ShouldMap() {
        Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
        Mapper.CreateMap<Source, Destination>().ConstructUsingServiceLocator();

        var destination =
        Mapper.Map<Source, Destination>(
        new Source { Value1 = "15", Value2 = "01/01/2000", }, 
            options => options.ConstructServicesUsing(
                type => new SourceDestinationTypeConverter())
        ); // exception is thrown here

        Assert.AreEqual(destination.Value2.Year, 2000);
    }
}

但是在断言发生之前我已经得到了一个异常:

System.InvalidCastException : Unable to cast object of type 'SourceDestinationTypeConverter ' to type 'Destination'.

我现在的问题是,如何使用自定义类型转换器ConstructServicesUsing()

4

2 回答 2

9

我测试了这段代码并使用以下代码使其工作:

public void TestMethod1()
    {
        Mapper.CreateMap<string, DateTime>().ConvertUsing(new DateTimeTypeConverter());
        Mapper.CreateMap<string, Type>().ConvertUsing(new StringTypeConverter());
        Mapper.CreateMap<string, int>().ConvertUsing(new StringIntConverter());
        Mapper.CreateMap<Source, Destination>();

        var destination =
        Mapper.Map<Source, Destination>(
        new Source { Value1 = "15", Value2 = "01/01/2000", Value3 = "System.String" },
            options => options.ConstructServicesUsing(type => new SourceDestinationTypeConverter())
        );

        Assert.AreEqual(destination.Value2.Year, 2000);
    }

还有额外的转换器:

 public class StringTypeConverter : ITypeConverter<string, Type>
{
    public Type Convert(ResolutionContext context)
    {
        return Type.GetType(context.SourceValue.ToString());
    }
}

public class StringIntConverter : ITypeConverter<string, int>
{
    public int Convert(ResolutionContext context)
    {
        return Int32.Parse(context.SourceValue.ToString());
    }
}

Automapper 缺少 String 到 Type 和 String 到 Int 的映射。我还必须删除以下行

Mapper.CreateMap<Source, Destination>().ConstructUsingServiceLocator();

并将其替换为

Mapper.CreateMap<Source, Destination>();

我不知道“ConstructUsingServiceLocator()”选项,但在这种情况下将其排除在外......(我不知道将其排除在外是否会给您带来其他问题。直到现在我还没有使用这个使用 Automapper 时的选项。)

请注意,我必须添加“Value3”参数,因为转换会失败......将 NULL 值转换为 Type 可能非常困难......(而且我不知道这里必须发生什么样的转换...... .)

于 2013-06-23T22:21:08.540 回答
0

我遇到了以下错误

InvalidCastException:在执行 CustomTypeConverter 的 Automapper 示例时,无法将类型为“xxxTypeConverter”的对象转换为类型“AutoMapper.ITypeConverter`2[System.String,System.DateTime]”。

我通过更换解决了这个问题

public class DateTimeTypeConverter : ITypeConverter<string, DateTime>

 public class DateTimeTypeConverter : AutoMapper.ITypeConverter<string, DateTime>

同样地

public class TypeTypeConverter : ITypeConverter<string, Type>

public class TypeTypeConverter : AutoMapper.ITypeConverter<string, Type>
于 2019-08-23T11:00:34.713 回答