I am learning generics in C#. So this may be simple for experienced folks.
I have 71 different models and I want to be able to generically store data from CSV into them.
The processing part is not super hard, I have this method signature:
private static async Task ProcessFileAsync<T>(string currentFile) where T : class, new()
The hard part is calling it. I have one CSV file for each model that I want to place data into. The Name of the CSV file is identical to the Model's name (ex: Product.csv would correspond to the Product model).
Ideally, I would like to just send the name in the caller, but I am getting a "X is a variable but is used like a type" Compiler error.
I could have a massive switch statement to solve this issue, but that seems relatively wasteful.
Any assistance would be appreciated.
Put another way, I could do the following:
switch(justFName)
{
case "Address":
_ = ProcessFileAsync<Address>(ci.FullName);
break;
case "Currency":
_ = ProcessFileAsync<Currency>(ci.FullName);
break;
...
...
...And so on
...
...
default:
//No method for this file name
break;
}
instead I would like to have something like this:
_ = ProcessFileAsync<justFName>(ci.FullName);