假设我声明了以下内容:
我尝试使用了以下方法:
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?
IsAssignableFrom
。这是正确的用法:typeof(Dictionary<string, string>).IsAssignableFrom(new Dictionary<string, string>().GetType());
- khellang