C# AutoMapper-使用属性属性进行自定义类型转换
我正在尝试确定是否可以使用可用属性(例如ValueConverterAttribute)为映射使用自定义类型转换。例如C# AutoMapper-使用属性属性进行自定义类型转换,c#,automapper,C#,Automapper,我正在尝试确定是否可以使用可用属性(例如ValueConverterAttribute)为映射使用自定义类型转换。例如 public class Source { public string Value1 { get; set; } } [AutoMap(typeof(Source), Reverse = true)] public class Destination { [?? ValueCoverter ??] public CustomType Value1 {
public class Source
{
public string Value1 { get; set; }
}
[AutoMap(typeof(Source), Reverse = true)]
public class Destination
{
[?? ValueCoverter ??]
public CustomType Value1 { get; set; }
}
public class CustomType
{
private string _someValue { get; set; }
// Constructor
public CustomType(string someValue)
{
_someValue = someValue;
}
// Do some stuff with _someValue ...
}
。。。然后在我的映射器配置中
var config = new MapperConfiguration(cfg =>
{
cfg.AddMaps(typeof(Destination));
});
var mapper = config.CreateMapper();
Source source = new Source()
{
Value1 = "Some value"
};
Destination destination = mapper.Map<Destination>(source);
var config=new-MapperConfiguration(cfg=>
{
cfg.AddMaps(目的地类型);
});
var mapper=config.CreateMapper();
Source Source=新源()
{
Value1=“一些值”
};
Destination=mapper.Map(源);
我的问题是,我可以使用
[ValueConverter]
属性在Destination.Value1属性上应用自定义类型转换器吗?我似乎找不到任何使用ValueConverter属性的示例。感谢@LucianBargaoanu。。。我在单元测试中找到了一个例子(对于某些原因,我以前看不到)。然而,我的代码与单元测试非常相似,但在映射器配置代码中出现了NullReferenceException
错误。我不确定我遗漏了什么。从工作代码开始。然后,一步一步地添加一些东西,使它更像你的失败案例。那会给你一个责备。希望你能看到你是做错了smth还是AM错误。