C# 借助注入服务的AutoMapper地图字典
我需要这样的东西:C# 借助注入服务的AutoMapper地图字典,c#,dictionary,dependency-injection,automapper,C#,Dictionary,Dependency Injection,Automapper,我需要这样的东西: public myMappingProfile(IInjectableService myInjectableService) { CreateMap<Source, Destination>() .ForMember(dest => dest.DestinationDictionary, opt => opt.MapFrom(src => {
public myMappingProfile(IInjectableService myInjectableService)
{
CreateMap<Source, Destination>()
.ForMember(dest => dest.DestinationDictionary, opt => opt.MapFrom(src =>
{
var dict = new Dictionary<myEnum, string>();
foreach (var item in src.SourceDictionary)
{
dict.Add(item.Key, myInjectableService.BuildUrl(item.Value));
}
return dict;
}));
但Visual Studio抱怨:
无法将属性或索引器分配给--它是只读的
下一次尝试是CustomResolver:
.ForMember(dest => dest.TenantImages, opt => opt.MapFrom<CustomResolver>())
.ForMember(dest=>dest.TenantImages,opt=>opt.MapFrom())
公共类CustomResolver:IValueResolver>>
{
私有只读IInjectableService\u myInjectableService
public CustomResolver(IInjectableService myInjectableService)
{
_myInjectableService = myInjectableService;
}
public List<KeyValuePair<MyEnum, string>> Resolve(
Source source,
Destination destination,
List<KeyValuePair<MyEnum, string>> destMember,
ResolutionContext context)
{
destMember = new List<KeyValuePair<MyEnum, string>>();
foreach (var entry in source.SourceDictionary)
{
destMember.Add(new KeyValuePair<myEnum, string>(entry.Key, _myInjectableService.BuildUrl(entry.Value)));
}
return destMember;
}
}
公共CustomResolver(IInjectableService myInjectableService)
{
_myInjectableService=myInjectableService;
}
公开列表解析(
来源,,
目的地,
名单成员:,
决议(上下文)
{
destMember=新列表();
foreach(source.SourceDictionary中的var条目)
{
Add(新的KeyValuePair(entry.Key,_myInjectableService.BuildUrl(entry.Value));
}
返回成员;
}
}
但会引发以下异常:
System.MissingMethodException:未定义无参数构造函数
对于这个对象
我不知道如何将iInputableService放入CustomResolver
有没有办法解决这个问题?
谢谢。我认为您必须使用:
看见
public CustomResolver(IInjectableService myInjectableService)
{
_myInjectableService = myInjectableService;
}
public List<KeyValuePair<MyEnum, string>> Resolve(
Source source,
Destination destination,
List<KeyValuePair<MyEnum, string>> destMember,
ResolutionContext context)
{
destMember = new List<KeyValuePair<MyEnum, string>>();
foreach (var entry in source.SourceDictionary)
{
destMember.Add(new KeyValuePair<myEnum, string>(entry.Key, _myInjectableService.BuildUrl(entry.Value)));
}
return destMember;
}
}
// Add this mapping, if the name of the property in source and destination type differ.
CreateMap<Source, Destination>()
.ForMember(dest => dest.DestinationDictionary, opt => opt.MapFrom(src => src.SourceDictionary));
// Add this mapping to create an instance of the dictionary, filled by the values from the source dictionary.
CreateMap</*type of source dictionary*/, Dictionary<myEnum, string>>()
.ConvertUsing(src =>
{
var dict = new Dictionary<myEnum, string>();
foreach (var item in src)
{
dict.Add(item.Key, myInjectableService.BuildUrl(item.Value));
}
return dict;
}));
src.ToDictionary(item => item.Key, item => myInjectableService.BuildUrl(item.Value));