c# - concurrentQueue tryDequeue with unknown out result -


i'm trying build flexible method handle different kind of concurrentqueues, 'cause logic handle queue's same.

it looks bit this:

private void startdoworkbutton(object sender, eventargs e)     {         startdowork();     }      private concurrentqueue<testdata> myqueue = new concurrentqueue<testdata>();     private void startdowork()     {         dowork(myqueue, new testdata());     }      private void dowork(dynamic queue,object objecttype)     {         dynamic outresult = activator.createinstance(objecttype.gettype());         while(queue.trydequeue(out outresult))         {             //do         }     } 

the outresult seems of correct type, message:"the best overloaded method match 'system.collections.concurrent.concurrentqueue.trydequeue(out winformwith10pxnewproject1.testdata)' has invalid arguments"

thing is, works fine when this:

    private void dowork(dynamic queue,object objecttype)     {         testdata outresult;// hardcoded type         while(queue.trydequeue(out outresult))         {             //do         }     } 

is there way can work around hard coding of type? give me lot of flexibility methods i'm creating.

kind regards,

matthijs

i use generic method this:

private void dowork<t>(concurrentqueue<t> queue,object objecttype) {     t outresult;// generic type     while(queue.trydequeue(out outresult))     {         //do     } } 

this way can use type of concurrentqueue< t>.

and can call like: (same hardcoded veriant)

private void startdowork() {     dowork(myqueue, new testdata()); } 

if need < t> of specific basetype or interface, can create constrains on it:

private void dowork<t>(concurrentqueue<t> queue,object objecttype) t: idisposable {     t outresult;// generic type      while(queue.trydequeue(out outresult))     {         //do          outresult.dispose();     } } 

the idisposable example. you're able call dispose method (from idisposable)


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -