我正在构建一个通用的漂亮打印方法。我想要单独处理的特殊类型之一是
下面的代码总是在
KeyValuePair<TK,TV>
。为了将对象缩小到已知类型,我想将每个 KeyValuePair<TK,TV>
映射到 KeyValuePair<object, object>
。下面的代码总是在
proxy
的 Key
和 Value
属性中产生两个 null 值。Mapper.CreateMap(o.GetType(), typeof(KeyValuePair<object, object>));
var proxy = Mapper.Map<KeyValuePair<object, object>>(o);
相比之下,这个非通用版本可以按照预期工作:
Mapper.CreateMap(o.GetType(), typeof(DictionaryEntry));
var proxy = Mapper.Map<DictionaryEntry>(o);
为什么?
o
在这个阶段已经被测试为一个KeyValuePair<,>
。
我正在使用.NET 4.0上的AutoMapper 3.2.1.0。
DictionaryEntry
,然后再映射到KeyValuePair<object, object>
。这样的话,你就可以使用ConstructUsing
方法了。 - Andrew WhitakerDictionaryEntry
时,我只需静态构建一个KVP<object, object>
:-) - Mau