C# ValidationAttribute for enums
<p>I have the following enums:</p>

<pre><code>public enum FirstEnum : short
{
    Unknown = -1,
    Red = 2,
    Green = 3
}

public enum SecondEnum 
{
    Unknown = -1,
    Orange = 2,
    Apple = 3
}
</code></pre>

<p>I now want to write a data annotation attribute so I can validate properties. So say I have a class:</p>

<pre><code>public class MyClass
{
    [EnumValidation(AllowUnKnown = true)]
    public FirstEnum  First { get; set; }

    [EnumValidation(AllowUnKnown = false)]
    public SecondEnum  Second { get; set; }
}
</code></pre>

<p>and my validation attribute is as follows:</p>

<pre><code>public class EnumValidationAttribute : ValidationAttribute
{
    public bool AllowUnKnown { get; set; }

    public override bool IsValid(object aValue)
    {
        bool valid = true;
        int enumValue = (int) aValue;  //** Cannot do hard cast
        if (enumValue == -1 &amp;&amp; !AllowUnKnown)
                valid = false;

        return valid;
    }
}
</code></pre>

<p>so now he issue comes in that I can no longer do a hard cast of the enum member to an int as FirstEnum is a short. </p>

<p>So how do I safely determine the value for the enum member? (I cannot change the declaration of FirstEnum) </p>