Excluding Types in the Generic Constraints (Possible?)

前端 未结 1 1008
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 11:02

Is possible to exclude specific types from the set of possible types, that can be used in a generic parameter? If so how.

For example

Foo()         


        
相关标签:
1条回答
  • 2020-12-06 11:31

    Nope, you can't make one-off exclusions like that using type constraints. You can do it at runtime though:

    public void Foo<T>()
    {
         if (typeof(T) == typeof(bool))
         {
             //throw exception or handle appropriately.
         }
    }
    
    0 讨论(0)
提交回复
热议问题