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

后端 未结 6 674
太阳男子
太阳男子 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:51

    Specifying the type be MyClass, or derived from it, is a value check on the argument itself. It's like saying the hello parameter in

    void Foo(int hello) {...}
    

    must be between 10 and 100. It's not possible to check at compile time.

    You must use generics or check the type at run time, just like any other parameter value check.

    0 讨论(0)
  • 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();
       // ...
    }
    
    0 讨论(0)
  • 2020-12-11 03:54

    You can also use an extension method, which will be available for all objects convertible to MyClass:

    public static class MyClassExtensions
    {
        public static void Foo(this MyClass obj)
        {
           // ...
        }
    }
    

    And you can use it as if it were an ordinary method of an object:

    var x = new MyClass();
    x.Foo();
    
    0 讨论(0)
  • 2020-12-11 03:56

    If your method has to take a Type type as it's argument, there's no way to do this. If you have flexibility with the method call you could do:

    public void Foo(MyClass myClass)
    

    and the get the Type by calling .GetType().

    To expand a little. System.Type is the type of the argument, so there's no way to further specify what should be passed. Just as a method that takes an integer between 1 and 10, must take an int and then do runtime checking that the limits were properly adhered to.

    0 讨论(0)
  • 2020-12-11 04:08

    You can use the following:

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

    The call would be like the following:

    {
        ...
        Foo(someInstanceOfMyClass);
        ...
    }
    
    0 讨论(0)
  • 2020-12-11 04:15

    why don't you use

    public void foo<t>();
    

    instead?

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