-1

我有一个问题,我的 List 到 Dictionary 的映射不会将 Name 映射到 Key 属性。价值做得很好。实现如下。System.ArgumentNullException: '值不能为空。参数名称:key'

执行

_mapper.Map<List<MetaModel>, Dictionary<string, object>>(model.MemberPaymentVendor.Meta)

配置

config.NewConfig<MetaModel, KeyValuePair<string, object>>()
                .ConstructUsing(x => new KeyValuePair<string, object>(x.Name, x.Value));

       config.NewConfig<List<MetaModel>, Dictionary<string, object>>()
                 .MapWith(s => ToDictionary<MetaModel>(s));
    
public static Dictionary<string, object> ToDictionary<T>(List<T> source)
    {
        if (source == null)
            return new Dictionary<string, object>();

        return source.Select(v => v.Adapt<KeyValuePair<string, object>>()).ToDictionary(k => k.Key, v => v.Value);
    }
4

1 回答 1

0

当我试图找出它的原因时,我用下面的简单例子抓住了它。

var x = new KeyValuePair<string, object>();
var y = new List<KeyValuePair<string, object>>();
y.Add(x);
y.ToDictionary(k => k.Key, v => v.Value); // <--- Value cannot be null. (Parameter 'key')

你看到这里会发生什么吗?

return source.Select(v => v.Adapt<KeyValuePair<string, object>>()).ToDictionary(k => k.Key, v => v.Value);
于 2021-07-12T17:34:46.073 回答