I defined a following example class having one generic method DoSomething:
public static class MyClass
{
public static void DoSomething<T>(T val)
{
System.Diagnostics.Contracts.Contract.Requires(typeof(T).IsEnum);
}
}
And I have the following enum:
public enum MyEnum { A, B }
Now I invoke the method with an instance of my enum as follows:
class Program
{
static void Main(string[] args)
{
MyClass.DoSomething(MyEnum.A);
}
}
With static checking enabled in the code contracts, the following warning is displayed at the line where I invoke the method:
CodeContracts: requires unproven: typeof(T).IsEnum
Why is it unproven if it the value is known at compile-time?
EDIT
Since this is obviously not working, probably because Code Contracts do not understand the semantics of IsEnum
or val is Enum
(as well pointed out by Jon). I am interested, if there is any known way to do this kind of checks in code contracts?