我有一个这样的映射器:
CreateMap<Source, ICollection<Dest>>()
.ConvertUsing((src, dst, context) =>
{
return context.Mapper.Map<ICollection<Dest>>
(new SourceItem[] { src.Item1, src.Item2 ... }.Where(item => SomeFilter(item)),
opts => opts.Items["SomethingFromSource"] = src.Something);
});
CreateMap<SourceItem, Dest>()
.ForMember(d => d.Something, opts => opts.MapFrom((src, dst, dstItem, context)
=> (string)context.Items["SomethingFromSource"]));
这给了我一个异常(exception)
You must use a Map overload that takes Action<IMappingOperationOptions>
.好吧,我确实使用了 Map
执行此操作的过载。我还能怎么做?
最佳答案
这是因为这个变化:
https://github.com/AutoMapper/AutoMapper/pull/3150
您可以通过访问 ResolutionContext 的 Options 属性来获取项目。
更改 context.Items["SomethingFromSource"]
至 context.Options.Items["SomethingFromSource"]
.
当没有 Items 时,ResolutionContext
与 DefaultContext
相同.因此ResolutionContext.Items
属性将抛出异常。
但是,如果有,ResolutionContext.Items
与 DefaultContext
不一样.因此ResolutionContext.Items
将返回列表。
虽然 ResolutionContext.Options.Items
将始终返回列表,它不会抛出任何异常,无论它是否为空。我相信这就是错误消息的意思,因为 ResolutionContext.Options
是 IMappingOperationOptions
关于c# - 升级到 9 后无法访问自动映射器上下文项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58398824/