假设我们有一个实现IDictionary<string, string>的类型。它称为MyStringDictionary

我正在做一些属性/字段/方法反射,想知道该成员是否是我可以转换为IDictionary<string, object>的类型。

我知道typeof(IDictionary<string, string>).IsAssignableFrom(typeof(MyStringDictionary))将是正确的,因为两个通用参数都匹配。但是,我不会直接分配给<string, string>,而是会转换为<string, object>,如下所示:

public class MyStringDictionary : IDictionary<string, string> {
   // Notice that the class itself has no generic type arguments!
}

MyStringDictionary myStringDict = GetBigDictionary();
IDictionary<string, object> genericDict = myStringDict
   .ToDictionary(kvp => kvp.Key, kvp => (object) kvp.Value);


如何确定可以进行这种转换?

我当时在想可以查看它是否实现了IEnumerable<KeyValuePair<,>>,但是我又一次被我不知道Value的类型实参并且不需要知道它的事实所困扰。将被装箱到object

最佳答案

我当时在想,我可以看看它是否实现了IEnumerable<KeyValuePair<,>>


绝对是这样!现在,了解如何完成此操作的详细信息:查看myObj.GetType().GetInterfaces()的结果,并调用下面的方法。如果返回true,则将第二个和第三个参数设置为键的类型和值的类型。

private static bool IsEnumKvp(Type t, out Type k, out Type v) {
    k = v = null;
    if (!t.IsGenericType) return false;
    var genDef = t.GetGenericTypeDefinition();
    if (genDef != typeof(IEnumerable<>)) return false;
    var itemType = t.GenericTypeArguments[0];
    if (!itemType.isGenericType) return false;
    var genItemDef = itemType.GetGenericTypeDefinition();
    if (genItemDef != typeof(KeyValuePair<,>)) return false;
    var kvpTypeArgs = genItemDef.GenericTypeArguments;
    k = kvpTypeArgs[0];
    v = kvpTypeArgs[1];
    return true;
}


MyStringDictionary上调用此方法应产生一个true

foreach (var t : MyStringDictionary.GetType().GetInterfaces()) {
    Type keyType, valType;
    if (IsEnumKvp(t, out keyType, out valType)) {
        Console.WriteLine(
            "Your class implements IEnumerable<KeyValuePair<{0},{1}>>"
        ,   keyType.FullName
        ,   valType.FullName
        );
    }
}


请注意,此方法可能会返回true多种类型。

10-04 21:37
查看更多