Is it possible to enumerate which types that is “available” in a generic constraint?
T MyMethod<t>() where T : int, double, string
Why I want to do this is that I have a small evaluator engine and would like to
write code like this:
bool expression.Evaluate<bool>();
or
int expression.Evaluate<int>();
but i want to prohibit
MyCustomClass expression.Evalaute<MyCustomClass>();
It is not possible to restrict a generic argument to specific types.
As a workaround, you could provide a method for each type and forward the method calls to a common implementation:
On the other hand, have you thought about encoding the type the expression evaluates to in the Expression type? E.g.