MentalDesk.DuckType 2.0.0

dotnet add package MentalDesk.DuckType --version 2.0.0                
NuGet\Install-Package MentalDesk.DuckType -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MentalDesk.DuckType" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MentalDesk.DuckType --version 2.0.0                
#r "nuget: MentalDesk.DuckType, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MentalDesk.DuckType as a Cake Addin
#addin nuget:?package=MentalDesk.DuckType&version=2.0.0

// Install MentalDesk.DuckType as a Cake Tool
#tool nuget:?package=MentalDesk.DuckType&version=2.0.0                


This is a duck typing library for C#. It allows you treat any object as if it implements any interface, so long as it has all the members required to do so.

Whilst dynamic types in C# allow you to do this, they do so at the expense of static typing and so expose you to the possibility of runtime errors.

By contrast, MentalDesk.DuckType uses source generators to create adapters around the objects that you want to duck type, so any changes to the members of those classes that would break the interface contract will result in a compiler error from the generated source code.


To get started, add MentalDesk.DuckType to your project by running the following command:

dotnet add package MentalDesk.DuckType


Imagine you have the following types:

class Duck
    public void Quack() => Console.WriteLine("Quack!");

class Derek
    public void Quack() => Console.WriteLine("quack...");

public interface IDuck
    public void Quack();

Derek and Duck can both clearly quack, and you want to be able to do something like this:

var duck = new Duck();
var derek = new Derek();
foreach (var d in [duck, derek]) // <-- this wont compile

Using MentalDesk.DuckType you can add a couple of assembly attributes to your project:

[assembly: DuckType<Duck, IDuck>]
[assembly: DuckType<Derek, IDuck>]

These DuckType<TClass, TInterface> attributes will trigger the duck typing source generators.

And then you can do this:

var duck = new Duck();
var derek = new Derek();
IDuck[] ducks = [ duck.AsIDuck(), derek.AsIDuck() ];
foreach (var d in ducks)

Behind the scenes, MentalDesk.DuckType has generated adapter classes wrapping Duck and Derek and also created some AsIDuck() extension methods that allow you to easily convert these to IDuck instances and leverage any common properties or methods that you've defined in the IDuck interface. It's statically typed, so it's safe and you don't have to write loads of code to make it work.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 111 2/16/2024
1.0.1 105 2/12/2024
1.0.0 102 2/12/2024