如何确定Type是否为Dictionary<,>
?
目前我唯一确定的方法是事先知道类型参数。
例如:
var dict = new Dictionary<string, object>();
var isDict = dict.GetType() == typeof(Dictionary<string, object>; // This Works
var isDict = dict.GetType() == typeof(Dictionary<,>; // This does not work
但字典不一定会是 <string, object>
,那么我该如何检查它是否为字典而又不知道参数以及不必检查名称(因为我们也有其他包含单词 Dictionary
的类)?
Dictionary<,>
对象实现了IDictionary
接口,因此可能是typeof(IDictionary)
。 - Ash Burlaczenko