Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get IDictionaryEnumerator from generic IDictionary?

Tags:

c#

.net

linq

I have a dictionary as follows:

  IDictionary<string, string> dict;

How to create an enumerator that implements IDictionaryEnumerator (preferably using linq)?

like image 319
Piotr Czapla Avatar asked Nov 24 '25 09:11

Piotr Czapla


1 Answers

Must be missing something here, what about:

IDictionary obsoleteDict = dict as IDictionary;
if (obsoleteDict == null)
{
 //Do something here...
}
else
{
 return obsoleteDict.GetEnumerator();
}

(edit: yep, you have to cast it to the old non-generic interface)

edit2: see Pavel's comment below. A type implementing IDictionary<K,V> may or may not implement IDictionary (Dictionary<K,V> does while some implementations like WCF's MessageProperties do not) so the cast may not work.

like image 195
Yann Schwartz Avatar answered Nov 26 '25 23:11

Yann Schwartz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!