Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to take in any Enum [duplicate]

Tags:

c#

Possible Duplicate:
Create Generic method constraining T to an Enum

is it possible to create a generic method that takes in any enum? I'll then check the incoming type to first make sure it's an enum that was passed (or can I enforce that natrually through the method definition?) and second then if it's an enum, I will have a bunch of case statements that do stuff based on what type of enum that was passed. So for example I can pass it CompanyColumns, PayColumns, etc. which are enums. My method needs to be able to take any enum like this and allow me to then work with the enum in my internal case statement.

public static DbType GetColumnDataType(I want to be able to pass in any object that's an enum)

like image 334
PositiveGuy Avatar asked Dec 11 '22 21:12

PositiveGuy


1 Answers

public static void MyFunction<T>(T en) where T: IComparable, IFormattable, IConvertible
{
    if (!typeof(T).IsEnum)
        throw new ArgumentException("en must be enum type");
    // implementation
}
like image 96
Denis Avatar answered Dec 29 '22 11:12

Denis