C# AutoMapper 自定义类型转换使用 ConstructServicesUsing
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17241406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AutoMapper custom type convert using ConstructServicesUsing
提问by Alexander Zeitler
According to the AutoMapper Documentation, I should be able to create and use an instance of a Custom Type Converter using this:
根据AutoMapper 文档,我应该能够使用以下方法创建和使用自定义类型转换器的实例:
var dest = Mapper.Map<Source, Destination>(new Source { Value = 15 },
opt => opt.ConstructServicesUsing(childContainer.GetInstance));
I have the following source and destination types:
我有以下源和目标类型:
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; }
}
And the following type converters:
以及以下类型转换器:
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;
}
}
This simple test should assert that one of the date properties get converted correctly:
这个简单的测试应该断言日期属性之一被正确转换:
[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);
}
}
But I already get an exception before the assert happens:
但是在断言发生之前我已经得到了一个异常:
System.InvalidCastException : Unable to cast object of type 'SourceDestinationTypeConverter ' to type 'Destination'.
System.InvalidCastException : Unable to cast object of type 'SourceDestinationTypeConverter ' to type 'Destination'.
My question now is, how do I use a custom type converter using ConstructServicesUsing()?
我现在的问题是,如何使用自定义类型转换器ConstructServicesUsing()?
采纳答案by Frederik Prijck
I tested this code and got this working using the following code:
我测试了这段代码并使用以下代码使其正常工作:
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);
}
And the extra Converters:
以及额外的转换器:
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 was missing a mapping for String to Type and String to Int. Also I had to remove the following line
Automapper 缺少 String 到 Type 和 String 到 Int 的映射。我也不得不删除以下行
Mapper.CreateMap<Source, Destination>().ConstructUsingServiceLocator();
and replace it by
并将其替换为
Mapper.CreateMap<Source, Destination>();
I'm not aware of the "ConstructUsingServiceLocator()" option, but leaving it out works in this case... (I have no idea whether leaving it out will bring up other issues for u. Until now I have not yet used this option when working with Automapper.)
我不知道“ConstructUsingServiceLocator()”选项,但在这种情况下将其忽略不计...(我不知道忽略它是否会给你带来其他问题。直到现在我还没有使用它使用 Automapper 时的选项。)
Please note I had to add the "Value3" parameter since the convertion would fail... Converting a NULL value to a Type can be pretty hard... (And I am not aware of what kind of conversion has to happen here...)
请注意,我必须添加“Value3”参数,因为转换会失败......将 NULL 值转换为类型可能非常困难......(我不知道这里必须发生什么样的转换...... .)
回答by Hemendr
I encountered error InvalidCastException: Unable to cast object of type 'xxxTypeConverter' to type 'AutoMapper.ITypeConverter`2[System.String,System.DateTime]' while executing Automapper sample for CustomTypeConverter. I fixed the issue by replacing
我在为 CustomTypeConverter 执行 Automapper 示例时遇到错误 InvalidCastException: Unable to cast object of type 'xxxTypeConverter' to type 'AutoMapper.ITypeConverter`2[System.String,System.DateTime]'。我通过替换解决了这个问题
public class DateTimeTypeConverter : ITypeConverter<string, DateTime>
with
和
public class DateTimeTypeConverter : AutoMapper.ITypeConverter<string, DateTime>
and similarly
同样
public class TypeTypeConverter : ITypeConverter<string, Type>
with
和
public class TypeTypeConverter : AutoMapper.ITypeConverter<string, Type>

