110,535
社区成员
发帖
与我相关
我的任务
分享
我定义了一个Dictionary<string,string> s的属性。 然后我s.getType();获得它的Type对象。 怎么判断他是Dictionary呢?
Dictionary<string, int> dic = new Dictionary<string, int>();
if (dic.GetType().IsGenericType && dic.GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
......
}
var x = s.GetType();
var t = typeof(Dictionary<,>);
var test = x.IsGenericType && x.GetGenericTypeDefinition() == t;
if (s is IDictionary) { } 或 if (typeof(IDictionary).IsAssignableFrom(s.GetType())) { }
s.GetType().Name.Contains("Dictionary")
if (s is IDictionary)
{
}
或
if (typeof(IDictionary).IsAssignableFrom(s.GetType()))
{
}
s.GetType().Name.Contains("Dictionary")