我正在使用反射和递归进行一些通用对象比较。递归方法在每个步骤中都需要一些类型信息,这些信息由调用者提供。有一次我知道下一个属性是 Dictionary<T,U> ,我想发送正确的类型。我想到了这个:
Type dictionaryType = typeof (IDictionary<,>).MakeGenericType(new [] {keyType.PropertyType, typeof(ValueType)});
在哪里keyType和 ValueType是较早发现的类型。但是,此类型未实现 IDictionary<KeyType, ValueType>接口(interface),根据 dictionaryType.GetInterfaces() .为什么?它看起来应该...
请您参考如下方法:
因为类型是 IDictionary<KeyType, ValueType> . GetInterfaces方法只 promise 返回 An array of Type objects representing all the interfaces implemented or inherited by the current Type .自 IDictionary<>不(实际上不能)实现自身,返回值合法地是它继承的所有接口(interface)。
使用 Dictionary<,>作为实现 IDictionary<,> 的任意类,以下将更合适:
Type dictionaryType = typeof (Dictionary<,>).MakeGenericType(new [] {keyType.PropertyType, typeof(ValueType)});
