可以說,我聲明如下如何確定一個對象實現IDictionary的或任何類型的IList的
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>))
所有這一切都返回假的!
那麼我如何確定(在這種情況下)項目實現IDictionary或IList?
看看這個:[如何確定一個類型是否實現了一個特定的泛型接口類型](http://stackoverflow.com/questions/503263/how-to-determine-if-a-type -implements-a-specific-generic-interface-type) – Zbigniew
@DaveZych,那麼我該如何去檢測該項目是否使用任何泛型來實現IDictionary或IList? – series0ne
您正在使用'IsAssignableFrom'錯誤的方式。這是真的:'typeof(Dictionary)。IsAssignableFrom(new Dictionary ()。GetType());' –
khellang