INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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.

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

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close