2

I have a private static function that has a string parameter and a Type parameter. I'm passing these to my code. However, I'm caught up on one specific area.

Here's my function:

private static object GetCSVRecords(string path, Type t)
{
    using (var txtReader = new StreamReader(path))
    {
        var csv = new CsvReader(txtReader);
        var recordList = csv.GetRecords<t>();
    }
    return recordList;
}

I'm attempting to pass a Type into the GetRecords<>. The error i'm getting says Cannot resolve symbol 't'. What am I doing wrong?

Turp
  • 708
  • 3
  • 14
  • 26
  • Can you change your method to a generic one? GetCSVRecords(string path) – Biscuits Mar 27 '15 at 12:31
  • possible duplicate of [How to use reflection to call generic Method?](http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method) – Eugene Podskal Mar 27 '15 at 12:35

4 Answers4

3

The problem is that t isn't a type (as required in the generic call), it's a reference to a Type object.

You can make your method generic:

private static object GetCSVRecords<T>(string path)
{
    using (var txtReader = new StreamReader(path))
    {
        var csv = new CsvReader(txtReader);
        var recordList = csv.GetRecords<T>();
        return recordList;
    }
}

Side note: You have to return the value inside the using block where the variable is declared (or declare it using a specific type outside the block).

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
1

Extending on Guffa's answer.

You can also fix the error by opting for the non-generic method overload of GetRecords, which better suits the method signature you're already using.

private static object GetCSVRecords(string path, Type t)
{
    using (var txtReader = new StreamReader(path))
    {
        var csv = new CsvReader(txtReader);
        return csv.GetRecords(t);
    }
}

Alternatively, you can change your method to use a generic type argument accordingly.

Biscuits
  • 1,767
  • 1
  • 14
  • 22
0

Others have mentioned using a generic type T in the method you're calling. Note that you can also set T at the class level:

class YourClass<T>{

    private static object GetCSVRecords(string path)
    {
        using (var txtReader = new StreamReader(path))
        {
            var csv = new CsvReader(txtReader);
            var recordList = csv.GetRecords<T>();
            return recordList;
        }
    }
}

In the case of static methods, this won't give you a whole lot more, as you have to specify the type for each call anyway, like:

YourClass<string>.GetCSVRecords(...);

It will let you use the same type T in various methods without specifying it there though.

If your method(s) were not static however, you could set the type once, when instantiating the class:

var yourObj = new YourClass<string>();
yourObj.GetCSVRecords(...): // Will use string as the type 
yourObj.DoSomethingElse(); // Could hypothetically also use T internally.
Kjartan
  • 18,591
  • 15
  • 71
  • 96
-1

You can only pass a type as generic argument when you have the actual type. You should refactor your method to use a generic type parameter instead:

private static object GetCSVRecords<T>(string path)
{
    using (var txtReader = new StreamReader(path))
    {
        var csv = new CsvReader(txtReader);
        var recordList = csv.GetRecords<T>();
    }
    return recordList;
}
Richard
  • 29,854
  • 11
  • 77
  • 120