What is Interface Duck Typing?

后端 未结 5 1563
深忆病人
深忆病人 2020-12-08 07:29

I heard the word Interface Duck Typing, but do not understand at all what is it? So I read a wiki about this and they said:

In computer programming wi

5条回答
  •  渐次进展
    2020-12-08 08:00

    C# has a nominal type system, so the compatibility of types is done based on their names. In your example you have two classes with a Quack method, however there is no way to write a method which can take instances of these two classes and invoke their Quack method.

    In C# 2, the solution would be to introduce an interface and have both classes implement it:

    public interface IQuack
    {
        void Quack();
    }
    
    public class Duck : IQuack { }
    public class Human : IQuack { }
    

    now you can create a method which take an IQuack instance and can call Human.Quack and Duck.Quack through it. In C#, methods are resolved 'early' at compile time, so you need to create a named type which supports the operations the method need so the compilation can succeed. Note there is still a runtime element to calling these methods, since the real implementation of IQuack.Quack needs to be resolved at runtime depending on the real type of the argument.

    In a duck-typing system, no attempt is made to validate that a method exists before runtime. All that is required is that a given object supports the operation in that it has the right name and takes the required number of parameters (none in this case), hence the 'if it quacks like a duck' expression.

    Duck typing in C# 2 can only be done using reflection, in this case you would accept an object argument and look for the required methods yourself:

    public static void MakeQuack(object duck)
    {
        MethodInfo quackMethod = duck.GetType().GetMethod("Quack", Type.EmptyTypes, null);
        if (quackMethod!=null)
        {
            quackMethod.Invoke(duck, new object[] { });
        }
        else
        {
            throw new ArgumentException("No Quack() method found on target");
        }
    }
    

    C#4 makes this much simpler with dynamic:

    public static void MakeQuack(dynamic duck)
    {
        duck.Quack();
    }
    

提交回复
热议问题