none
Create Generic method constraining T to an Enum RRS feed

  • Question

  • Hi guys,

    I have a method:

     

    public void TestMetod<T>(T enumValue): where T: struct, IConvertible
    {
    if (!typeof(T).IsEnum)
    {
    throw new ArgumentException("wrong type");
    } 
    .....
    .....
    .....
    }
    
    //enumValue could be one of the two Enums:
    [Flags]
    public enum TestEnumFirst
    {
    testValueOne,
    testValueTwo
    }
    
    [Flags]
    public enum TestEnumSecond
    {
    testValueOne,
    testValueTwo
    }
    
    

     

    How can I recognize from which Enum I have the value (from TestEnumFirst or TestEnumSecond ) to use in a way:

     

    private TestEnumFirst testEnumFirst;
    private TestEnumSecond testEnumSecond;
    
    
    //if enumValue is from TestEnumFirst 
    
    testEnumFirst = testEnumFirst | enumValue;
    //Or
    testEnumFirst = testEnumFirst ^ enumValue;
    
    //else if enumValue is from TestEnumSecond
    
    testEnumSecond = testEnumSecond | enumValue;
    //Or
    testEnumSecond = testEnumSecond ^ enumValue;  
    

    thank u in advance,

    J.

    Saturday, December 18, 2010 3:05 PM

Answers

All replies