Thursday, 11 May 2017

c# - Method to take in any 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)


Answer



public static void MyFunction(T en) where T: IComparable, IFormattable, IConvertible
{
if (!typeof(T).IsEnum)
throw new ArgumentException("en must be enum type");
// implementation
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...