【发布时间】:2012-10-23 08:19:08
【问题描述】:
假设我声明以下内容
Dictionary<string, string> strings = new Dictionary<string, string>();
List<string> moreStrings = new List<string>();
public void DoSomething(object item)
{
//here i need to know if item is IDictionary of any type or IList of any type.
}
我尝试过使用:
item is IDictionary<object, object>
item is IDictionary<dynamic, dynamic>
item.GetType().IsAssignableFrom(typeof(IDictionary<object, object>))
item.GetType().IsAssignableFrom(typeof(IDictionary<dynamic, dynamic>))
item is IList<object>
item is IList<dynamic>
item.GetType().IsAssignableFrom(typeof(IList<object>))
item.GetType().IsAssignableFrom(typeof(IList<dynamic>))
全部返回false!
那么我如何确定(在这种情况下)项目实现 IDictionary 或 IList?
【问题讨论】:
-
@DaveZych,那么我该如何检测该项目使用任何泛型类型实现 IDictionary 或 IList?
-
您以错误的方式使用
IsAssignableFrom。这是真的:typeof(Dictionary<string, string>).IsAssignableFrom(new Dictionary<string, string>().GetType()); -
请问使用场景?