1

I have a generic function as below:

  public async Task<List<T>> GetAllPagedAsync<T>(int limit, int offset, string tableName, int month, int year)
  {
     return await _dapperRepository.GetAllAsync<T>($"Select * From {tableName} Where Month(CreatedDate) = {month} And Year(CreatedDate) = {year} Limit {limit} Offset {offset}");
  }

Instead of giving the type directly to the function, I want to fetch the type with a class name and give it to the function like this:

string className = FetchClassName();
GetAllPagedAsync<className>(limit,offset,tableName,month,year);

Because in the structure I set up here, I will get the class name from appsettings.

Nigel
  • 2,192
  • 1
  • 8
  • 23
  • 2
    _" like this"_ - like what? – Guru Stron Nov 03 '21 at 19:59
  • 4
    please use ***parameterised queries*** - building SQL queries by concatenation etc. is a recipe for disaster. not only is it a source for many hard to debug syntax errors, it's also a wide, open gate for ***[SQL Injection attacks](https://bobby-tables.com/)***. – Franz Gleichmann Nov 03 '21 at 19:59
  • 1
    I flagged and downvoted because this question needs details and clarity, as Guru Stron already mentioned – Nigel Nov 03 '21 at 20:01
  • 1
    Maybe [this](https://stackoverflow.com/q/232535/2501279) will help. – Guru Stron Nov 03 '21 at 20:01
  • Look up `MakeGenericMethod` (I think that is what you are asking). Stackoverflow has many examples. You can't directly do something like `GetAllPagedAsync`; you need to use reflection – Flydog57 Nov 03 '21 at 20:02

0 Answers0