C# Get Type of IEnumerable<TModel>
- by Jimbo
I have a method to which I pass an IEnumerable<TModel>. Then depending on the type of TModel, the method carries out a set of instructions as below:
public void MyMethod<TModel>(IEnumerable<TModel> items) where TModel : class
{
int operationType;
switch (typeof(TModel))
{
case typeof(MyModelOne):
operationType = 1;
break;
case typeof(MyModelTwo):
operationType = 2;
break;
case typeof(MyModelThree):
operationType = 3;
break;
default:
throw new Exception("The collection model passed to MyMethod is not recognized");
}
...
}
This doesnt work, I get the error:
There is no application variable or memeber 'TModel'