C# Get Type of IEnumerable<TModel>

Posted by Jimbo on Stack Overflow See other posts from Stack Overflow or by Jimbo
Published on 2010-05-21T13:44:41Z Indexed on 2010/05/21 13:50 UTC
Read the original article Hit count: 355

Filed under:
|
|
|
|

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'

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics