Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将IDictionary映射到IDictionary时出现奇怪的自动映射器定义_C#_.net Core_Automapper - Fatal编程技术网

C# 将IDictionary映射到IDictionary时出现奇怪的自动映射器定义

C# 将IDictionary映射到IDictionary时出现奇怪的自动映射器定义,c#,.net-core,automapper,C#,.net Core,Automapper,伙计们 当我试图将一个IDictionary映射到IDictionary或IDictionary映射到IDictionary时,我对Automapper有一个奇怪的定义 那么,让我们有两个类: public class OrderProfile { public IDictionary<int, int> CountableAuxElements { get; set; } } public class OrderProfileResponse { publi

伙计们

当我试图将一个
IDictionary
映射到
IDictionary
IDictionary
映射到
IDictionary
时,我对Automapper有一个奇怪的定义

那么,让我们有两个类:

public class OrderProfile 
{
    public IDictionary<int, int> CountableAuxElements { get; set; } 
}

public class OrderProfileResponse 
{
    public IDictionary<int, int> CountableAuxElements { get; set; }
    public IDictionary<string, int> CountableAuxElementsString { get; set; } 
}
我试图编写如下代码:

Unable to cast object of type 'System.Collections.Generic.List'1[System.Collections.Generic.KeyValuePair'2[System.Int32,System.Int32]]' to type 'System.Collections.Generic.IDictionary`2[System.Int32,System.Int32]'.
CreateMap<OrderProfile, OrderProfileResponse>().ForMember(e => e.CountableAuxElements, f => f.MapFrom(s => s.CountableAuxElements.ToDictionary(f => f.Key, f => f.Value)))
CreateMap().formMember(e=>e.CountableAuxements,f=>f.MapFrom(s=>s.CountableAuxements.ToDictionary(f=>f.Key,f=>f.Value)))
或者这个:

CreateMap<OrderProfile, OrderProfileResponse>().ForMember(e => e.CountableAuxElements, opt => opt.ConvertUsing(new DictionaryConversation<int, int, int, int>((p, context) => p, (p, context) => p), src => src.CountableAuxElements))
CreateMap().ForMember(e=>e.countableauxelents,opt=>opt.ConvertUsing(新字典转换((p,context)=>p,(p,context=>p),src=>src.countableauxelents))
其中:

internal class DictionaryConversation<TOldKey, TOldValue, TNewKey, TNewValue> : IValueConverter<IDictionary<TOldKey, TOldValue>, IDictionary<TNewKey, TNewValue>>
{
    private readonly Func<TOldKey, ResolutionContext, TNewKey> keyModifier;
    private readonly Func<TOldValue, ResolutionContext, TNewValue> valueModifier;

    public DictionaryConversation(Func<TOldKey, ResolutionContext, TNewKey> keyModifier, Func<TOldValue, ResolutionContext, TNewValue> valueModifier)
    {
        this.keyModifier = keyModifier;
        this.valueModifier = valueModifier;
    }

    public virtual IDictionary<TNewKey, TNewValue> Convert(IDictionary<TOldKey, TOldValue> sourceMember, ResolutionContext context) =>
               sourceMember.ToDictionary(e => keyModifier(e.Key, context), e => valueModifier(e.Value, context));
}
内部类字典转换:IValueConverter
{
私有只读Func键修改器;
私有只读Func valueModifier;
公共字典转换(Func键修饰符、Func值修饰符)
{
this.keyModifier=keyModifier;
this.valueModifier=valueModifier;
}
公共虚拟IDictionary转换(IDictionary sourceMember,ResolutionContext上下文)=>
ToDictionary(e=>keyModifier(e.Key,context),e=>valueModifier(e.Value,context));
}
但同样的错误。 当通过
到字典
字典会话
将CountableAuxElementsString映射时,我遇到了与CountableAuxElementsString相同的错误

这个错误的原因是什么?我的头真的碎了


谢谢。

请检查。它也适用于字典。@LucianBargaoanu所以,如果我没弄错的话,Automapper不能处理IDictionary实现类型,对吗?我把你的代码一对一地粘贴了下来,无法重现你的错误。记得发一封邮件。还有,如果源对象中没有具有此名称的成员,为什么还要麻烦使用
Ignore()
作为
CountableAuxElementsString
?对不起,伙计们,我发现了我的错误。它在配置代码中是这样的:
cfg.CreateMap(typeof(ICollection),typeof(ICollection)).includealderived().ConvertUsing(typeof(CollectionTypeConverter))和这部分代码损坏了我。对不起,是我的错。。。
internal class DictionaryConversation<TOldKey, TOldValue, TNewKey, TNewValue> : IValueConverter<IDictionary<TOldKey, TOldValue>, IDictionary<TNewKey, TNewValue>>
{
    private readonly Func<TOldKey, ResolutionContext, TNewKey> keyModifier;
    private readonly Func<TOldValue, ResolutionContext, TNewValue> valueModifier;

    public DictionaryConversation(Func<TOldKey, ResolutionContext, TNewKey> keyModifier, Func<TOldValue, ResolutionContext, TNewValue> valueModifier)
    {
        this.keyModifier = keyModifier;
        this.valueModifier = valueModifier;
    }

    public virtual IDictionary<TNewKey, TNewValue> Convert(IDictionary<TOldKey, TOldValue> sourceMember, ResolutionContext context) =>
               sourceMember.ToDictionary(e => keyModifier(e.Key, context), e => valueModifier(e.Value, context));
}