0

I have a generic method, so:

public void Export<T>(List<T> exportList, string filePath, byte fileType) where T: class

There are two possibilities for <T>: <Category> and <ProductSupplier>. I don't know which one I am using in a given runtime instance; which one to use is passed as a parameter to the method calling this method.

Is it possible to pass a variable value to <T> during runtime? I've played around with GetMethod/Invoke, etc. (I've already gotten that to work with a non-generic method), but they seem to address the method itself, rather than working as a mechanism to vary the generic List type.

[Edit]As it turns out, my question has its basis in a misunderstanding. When I first got my Export method to work at all, it was with this format:

public void Export<Category>(someCategoryList, "C:\Temp2\myFile.txt", 0) 

I wanted to know how I could plug different values in for <Category> without explicitly doing so at design time with some sort of ever-expanding switch statement. As it turns out, this works fine:

public void Export(someCategoryList, "C:\Temp2\myFile.txt", 0) 

so the problem is moot. I've already been able to derive the right List ("someCategoryList" in my example) using Reflection, so problem solved.

BobRodes
  • 5,990
  • 2
  • 24
  • 26
  • _"inject either of the two possibilities for < T >"_ - why do you need "injecting"? Why not writing `Export` method to be able to process both `Category` and `ProductSupplier`? What does `Export` do? – Konrad Kokosa Jul 28 '14 at 14:42
  • Are you sure that you need reflection and dynamics for your program? Well, if it really is, then you should probably read http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method. – Eugene Podskal Jul 28 '14 at 14:42
  • @KonradKokosa I can write the method to be able to process both, but I'd rather be able to plug any sort of dataset object into a template. – BobRodes Jul 28 '14 at 15:14
  • @EugenePodskal I'm really just interested in learning how to do this. I've read that post several times, as well as quite a number of other posts/blogs of Skeet's, but I haven't figured out how to apply it to this situation yet. I'll have another go. And Lasse, I've seen probably 20 related posts that are marked as duplicates, and still haven't resolved the problem. I'll see if I can edit it to be more specific. Thank you all for your input. – BobRodes Jul 28 '14 at 15:21
  • @BobRodes It is commendable that you have read many information sources, but the first thing before solving the problem is to determine your goal and environment - usage scenario. If you don't actually understand what you want to achieve then most of those you ask will fail to understand your goal(problem) or misinterpret it. So, try to create [minimal complete verifiable example](http://stackoverflow.com/help/mcve). It will show your usage scenario and the moment where you have problems. – Eugene Podskal Jul 28 '14 at 15:31
  • @BobRodes [minimal complete verifiable example](http://stackoverflow.com/help/mcve) - concrete scenario of usage(of course omitting the moment you don't know how to write). [This](http://stackoverflow.com/questions/557340/how-to-get-the-type-of-t-from-a-generic-listt) (just read second answer) and [this](http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method) will provide you with all basic ideas for your mvce. If something will be still hazy - ask new question. – Eugene Podskal Jul 28 '14 at 16:22
  • @EugenePodskal See my edits above, which should clarify what I was trying to ask, and why nobody seemed to understand clearly what I was talking about. Perhaps this question isn't really a duplicate, since probably nobody else has managed to invent the exact same non-issue. – BobRodes Jul 28 '14 at 18:46
  • @BobRodes I am not sure that it qualifies as mcve, but if you have clean and concise question, you can ask it here -(http://stackoverflow.com/questions/ask), because this question is closed and reopening it wouldn't be worth the efforts spent. http://stackoverflow.com/help/mcve is your best friend. Goodbye and good luck. – Eugene Podskal Jul 28 '14 at 19:05

0 Answers0