Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to vary the type of a List<T> method parameter at runtime?

Status
Not open for further replies.

BobRodes

Instructor
May 28, 2003
4,215
US
I have a generic method, so:

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

There are two possibilities for <T>: <Category> and <ProductSupplier>. Previous to this call, I have a method which uses reflection to get a List<T> object (at least, I think it is; Invoke returns an object object and setting myList to dynamic allows me to access the data properties), populated with either Category or ProductSupplier data:

Code:
dynamic myList = cRet.GetType().GetMethod(parms.data).Invoke(cRet, null)

where cRet is an object with GetCategories() and GetProductSuppliers() methods, and parms.data is a string with either "GetCategories" or "GetProductSuppliers" as a value.

So, what I would like to be able to do is inject either of the two possibilities for <T> into the Export method at runtime. The parm object is a set of parameters that are passed into a worker method pursuant to a ParameterizedThreadStart call.

Is this doable?

An unforeseen consequence of the information revolution has been the exponential propagation of human error.
 
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:

Code:
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:

Code:
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.

An unforeseen consequence of the information revolution has been the exponential propagation of human error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top