Excluding Types in the Generic Constraints (Possible?)

前端 未结 1 1022
伪装坚强ぢ
伪装坚强ぢ 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()
    {
         if (typeof(T) == typeof(bool))
         {
             //throw exception or handle appropriately.
         }
    }
    

    0 讨论(0)
提交回复
热议问题