c# - how to convert ConcurrentDictionary to Dictionary at runtime? -


i know how convert concurrentdictionary dictionary.

and know can use reflection determine if object contains concurrentdictionary.

but after determined object have concurrentdictionary via reflection, how convert dictionary @ runtime? or can @ all? it's going change definition of class, right?

edit: should have made more clear. i'll show example:

    [serializable]     [datacontract]     public class cacheitem     {         [datamember]         private concurrentdictionary<string, cacheitementity> _cacheitemdictionary = new concurrentdictionary<string, cacheitementity>();          ......     } 

when serialize instance of class, avro can't serialize concurrentdictionary. wondered if can convert concurrentdictionary normal dictionary @ runtime. , changes definition of class. i'm wondering if can done way.

concurrentdictionary<tkey, tvalue> implements idictionary<tkey, tvalue> anyplace trying use "dictionary" can use interface instead. example:

void consumeidictionary(idictionary dic) {    //perform work on dictionary, regardless of concrete type } 

you call method , fine:

consumeidictionary(new concurrentdictionary<int,int>()); 

alternatively if have method want use requires concrete dictionary<tkey,tvalue> type, can use dictionary constructor takes existing idictionary:

void consumedictionary<k,v>(dictionary<k,v> dic) {    //perform work on concrete dictionary } 

then call this:

consumedictionary(    new dictionary(        new concurrentdictionary<int,int>())); 

just aware calling constructor o(n) operation.

if trying using reflection, can determine object concurrentdictionary examining object's type @ runtime via gettype():

bool isconcurrentdictionary<k, v>(obj o) {     return o.gettype() == typeof(concurrentdictionary<k,v>); } 

but in case may want forget generic type parameters , check idictionary interface:

bool isdictionary(obj o) {     return o idictionary; } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -