Diciamo dichiaro quanto segueCome determinare se un oggetto implementa IDictionary o IList di qualsiasi tipo
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.
}
Ho provato con:
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>))
Tutto ciò return false!
Quindi, come faccio a determinare che (in questo contesto) l'elemento implementa IDictionary o IList?
Date un'occhiata a questo: [Come determinare se un tipo implementa uno specifico tipo di interfaccia generica] (http://stackoverflow.com/questions/503263/how-to-determine-if-a-type -implements-a-specific-generic-interface-type) – Zbigniew
@DaveZych, Quindi come faccio a rilevare che l'elemento implementa IDictionary o IList con QUALSIASI tipo generico? – series0ne
Si sta utilizzando 'IsAssignableFrom' nel modo sbagliato. Questo è vero: 'typeof (Dizionario). IsAssignableFrom (nuovo dizionario (). GetType());' –
khellang