Duck Casting Framework / Duck Typing
(also sometimes incorrectly described as Latent Typing as Ian Griffiths explains in his campaign to disabuse that notion) for .NET languages
The term duck typing is popularly explained by the phrase
If it walks like a duck and quacks like a duck, it must be a duck.
In computer programming with object-oriented programming languages, duck typing is a style of typing in which an object's methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of an explicit interface. The name of the concept refers to the duck test.
Duck typing allows an object to be passed in to a method that expects a certain type even if it doesn’t inherit from that type. All it has to do is support the methods and properties of the expected type in use by the method. I emphasize that last phrase for a reason. Suppose we have a method that takes in a duck instance, and another method that takes in a rabbit instance. In a dynamically typed language that supports duck typing, I can pass in my object to the first method as long as my object supports the methods and properties of duck in use by that method. Likewise, I can pass my object into the second method as long as it supports the methods and properties of rabbit called by the second method. Is my object a duck or is it a rabbit? Like the above image, it’s neither and it’s both. In many (if not most) dynamic languages, my object does not have to support all methods and properties of duck to be passed into a method that expects a duck. Same goes for a method that expects a rabbit.It only needs to support the methods and properties of the expected type that are actually called by the method.
Please refer this to get an idea about Duck Typing
Here is one of the most commonly used nuget package used for duck casting:
ImpromptuInterface - NuGet Must Haves
A Lightweight Duck Casting Framework for dynamic C# 4.0 (.net4 & silverlight4 & silveright5 ).
Comments
Post a Comment