gpt4 book ai didi

c# - AutoMapper 自定义类型转换使用 ConstructServicesUsing

转载 作者:太空狗 更新时间:2023-10-30 00:42:07 25 4
gpt4 key购买 nike

根据AutoMapper Documentation ,我应该能够使用这个创建和使用自定义类型转换器的实例:

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:无法将“SourceDestinationTypeConverter”类型的对象转换为类型“Destination”

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

最佳答案

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

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 值转换为类型可能非常困难...(而且我不知道必须进行哪种类型的转换这里……)

关于c# - AutoMapper 自定义类型转换使用 ConstructServicesUsing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17241406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com