C#: Restricting Types in method parameters (not generic parameters)

后端 未结 6 673
太阳男子
太阳男子 2020-12-11 03:21

I\'d like to code a function like the following

public void Foo(System.Type t where t : MyClass)
{ ... }

In other words, the argument type

6条回答
  •  时光说笑
    2020-12-11 03:54

    What you want could theoretically be done with attributes. But this is much clearer (imo) and does exactly the same thing:

    public void Foo(MyClass m) {
       Type t = m.GetType();
       // ...
    }
    

提交回复
热议问题