I've done some more digging and the problem is that Nullable is actually a
struct. Accordingly templates can only be derived from interfaces,
non-sealed CLASSES and types. struct's cannot be used as base classes; full
stop...
You may be able to define your own class which implements the methods in
line with how the Nullable struct operates; it basically adds a boolean to
each type to indicate whether the value has been assigned or not... I cannot
see any reason why
this will not work but you may not be able to use the short hand form; i.e.
type? you may have to use MyType.Nullable<int> myInt... as I would assume
that the short hand form would be bound to System.Nullable (could be wrong
.
I cannot think of any reason why this will not work but unless I actually
tried to write the code could not guarantee it would be trouble free. I also
cannot think of any reason why Nullable is implemented as a struct aside
from a value type performance angle.
If you wrote your own Nullable class then:
class MyClass<T> where T : class
Would obviously work with both reference types and your new
MyNameSpace.Nullable class (as it would be a class) the only problem would
be that calling the methods on it would need an is check (i.e. if (t is
MyNameSpace.Nullable)...).
To avoid this you would need to define two classes, one for reference types
and the other for your Nullable type:
class MyRefClass<T> where T : class
class MyNullableClass<T> where T : MyNameSpace.Nullable
HTH
- Andy