A few days ago I asked a question titled How to constraint a generic to be of type enum?. To summarize the problem is the following code:
class MyClass<T> where T : enum // Not possible in C#
{
}
I was introduced to code contracts and that I could produce a compile time warning for the problem, which is all I want (to be informed at compile time that T
should be an enum
). I tried the follwing code (Full source).
class MyClass<T>
{
public MyClass()
{
Contract.Requires(typeof(System.Enum).IsAssignableFrom(typeof(T)));
}
}
It only produces a useless runtime error. I should be able to produce a compile time warning but I can't get it to work. Can anyone tell me what I'm doing wrong?
Here's a picture of project's Code Contracts setting: