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

后端 未结 6 696
太阳男子
太阳男子 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 04:08

    You can use the following:

    public void Foo(T variable) where T : MyClass
    { ... }
    

    The call would be like the following:

    {
        ...
        Foo(someInstanceOfMyClass);
        ...
    }
    

提交回复
热议问题