typeof(whatever)
always returns an instance of type Type
. Type
doesn't derive from Base
.
What you want is this:
if(typeof(T) == typeof(Base))
throw new NotSupportedException("Please specify a type derived from Base");
Something that looks like it is the same is this:
if(variableOfTypeT is Base)
But that has a different meaning.
The first statement (with typeof(Base)
) only is true
if T
is Base
. It will be false
for any type derived from Base
.
The second statement (variableOfTypeT is Base
) is always true
in your class, because any class derived from Base
will return true
for a check for its base class.