Sunday, 11 August 2013

Detect MaxValue of generic parameter

Detect MaxValue of generic parameter

I want to write generic class which should work with byte and ushort
types. What constraint should I use for this class? How can I detect
MaxValue property inside of this class?
class MyClass<T> // where T: ???
{
void Foo()
{
int maxValue = T.MaxValue; // how can I do this?
}
}
If class was created with unexpected type, which doesn't contain MaxValue
property, I don't care - for example, I can throw exception at runtime.

No comments:

Post a Comment