简体   繁体   English

从实现IDictionary <,>的类型中获取类型参数

[英]Get type parameters from type implementing IDictionary<,>

I would like to write a function to get the type parameters from types implementing IDictionary. 我想编写一个函数来从实现IDictionary的类型中获取类型参数。 What I have so far, and what is discussed in most SO questions is: 到目前为止,在大多数SO问题中讨论的是:

public static Type[] GetParameters(Type dicType) {

    if (typeof(IDictionary).IsAssignableFrom(dicType)) {
        return dicType.GetGenericArguments();
    }
    else {
        return null;
    }
}

However this fails with the following: 但是,以下操作失败:

public class MyClass : Dictionary<string, int> { }
Type[] typeParams = GetParameters(typeof(MyClass));
Console.WriteLine(String.Join(", ", (object[]) typeParams));

which would print nothing (empty array). 这将不打印任何内容(空数组)。 This is even worse with the following: 在以下情况下,情况甚至更糟:

public class MyOtherClass<T, U> : Dictionary<string, string> { }
Type[] typeParams = GetParameters(typeof(MyOtherClass<int, int>));
Console.WriteLine(String.Join(", ", (object[]) typeParams));

as the output is completely wrong (System.Int32, System.Int32). 因为输出完全错误(System.Int32,System.Int32)。

How can I get these parameters for any type that inherits from IDictionary ? 我如何获取从IDictionary继承的任何类型的这些参数?

While writing the question, I actually came up with an answer: 在写问题时,我实际上想出了一个答案:

public static Type[] GetDictionaryParameters(Type dicType) {
    foreach(var interf in dicType.GetInterfaces()) {
        if (interf.IsGenericType && interf.GetGenericTypeDefinition() == typeof(IDictionary<,>)) {
            return interf.GetGenericArguments();
        }
    }

    // No matching interface
    return null;
}

This works with everything I have so far. 这适用于我到目前为止的所有内容。 If you have a better idea, please post another answer ! 如果您有更好的主意,请发布另一个答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义类实现接口 IDictionary<type, type> 那可能是静态的? - Custom class implementing interface IDictionary<type, type> that could be static? 是否可以查看对象是否继承了没有泛型类型参数的IDictionary? - Is it possible to see if an object inherits IDictionary without generic type parameters? 嵌套 IEnumerable 的映射<idictionary>到不同类型的嵌套 IEnumerable<idictionary></idictionary></idictionary> - Mapping of nested IEnumerable<IDictionary> to a different type of nested IEnumerable<IDictionary> 类型参数的约束-根据类型从字典获取值 - Constraints on Type Parameters - get value from dictionary based on type 从 IDictionary 中获取所有条目 - Get all entries from an IDictionary 如何声明一个类型的(字符串,IDictionary<string, object> ) - How to declare a type of (string, IDictionary<string, object>) 强制 IDictionary 键在特定的 class 类型内 - Forcing IDictionary key to be within specific class type 为什么类型为ICollection的IDictionary.Values属性? - Why is the `IDictionary.Values` property of type `ICollection`? 将 ConcurrentDictionary 类型传递给采用 IDictionary 的 function 是否安全? - Is it safe to pass ConcurrentDictionary type into a function that takes IDictionary? 使用 roslyn 获取属性类型的类型参数 - Get type parameters of property type using roslyn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM