1

how can I pass a reference to a generic method?

I have the class below.

public static class DataTableUtil
{
    public static List<T> DataTableToList<T>(DataTable dt)
    {
        List<T> lstItems = new List<T>();
        if (dt != null && dt.Rows.Count > 0)
            foreach (DataRow row in dt.Rows)
                lstItems.Add(ConvertDataRowToGenericType<T>(row));
        else
            lstItems = null;
        return lstItems;
    }...

I'm consuming the class this way

var lista = DataTableUtil.DataTableToList<SasRating>(retorno).AsEnumerable();

The SasRating class is a POCO class. I would like to send this parameter dynamically through a factory pattern. It is possible ? How to do this ?

Here's an example of some (invalid) code that demonstrates what I'm looking for:

var genericObj = T;

if(T is SasRating)
{
     T = SasRating
}
else
{
     T = SasTest;
}

var lista = DataTableUtil.DataTableToList<T>(retorno).AsEnumerable();
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
Pedro
  • 238
  • 1
  • 4
  • 10
  • 2
    Please make an attempt at showing what you want to accomplish, even if the code is invalid, the keywords "dynamically" and "factory pattern" adds too many levels of indirection for me to clearly visualize what you want to accomplish. – Lasse V. Karlsen Jan 29 '19 at 12:43
  • It's not clear to me what the problem is here. What isn't working? – David Jan 29 '19 at 12:45
  • It'll probably involve something like [this](https://stackoverflow.com/q/232535/4137916). How you fix that up with your factory pattern is another matter. An alternative, if you are *never* using these methods statically (or almost never), is to pass along `Type` instances instead of using generics (this option is surprisingly often overlooked). – Jeroen Mostert Jan 29 '19 at 12:46
  • @JeroenMostert nails it, you will have to use reflection for this. – Lasse V. Karlsen Jan 29 '19 at 12:54
  • Basically, your title could be rewritten as "How can I use a variable for the T of a generic method?", which makes me understand clearer what you wanted to do, and yes, reflection is the answer here, as is @JeroenMostert's hint to use a `Type` parameter instead of a generic method. – Lasse V. Karlsen Jan 29 '19 at 12:55
  • What type of reference would to like to pass? – Magnus Jan 29 '19 at 12:55
  • 1
    Ignore this comment if it was intended this way, but I wouldn't return `null` if the datatable was empty. This might never blow up using test data, but might in a production environment. – C.Evenhuis Jan 29 '19 at 12:56
  • I'd almost forget, but it looks like your reinventing [Dapper](https://github.com/StackExchange/Dapper/blob/master/Readme.md). Consider using it -- it's far faster and more flexible than any code that converts data tables will ever be. `DataTable` is a lemon that's very uneconomical with memory -- it should only be used if you really don't know what you result sets will look like in advance, which is rarely. – Jeroen Mostert Jan 29 '19 at 13:12

0 Answers0