MentalDesk.DuckType
1.0.1
See the version list below for details.
dotnet add package MentalDesk.DuckType --version 1.0.1
NuGet\Install-Package MentalDesk.DuckType -Version 1.0.1
<PackageReference Include="MentalDesk.DuckType" Version="1.0.1" />
paket add MentalDesk.DuckType --version 1.0.1
#r "nuget: MentalDesk.DuckType, 1.0.1"
// Install MentalDesk.DuckType as a Cake Addin #addin nuget:?package=MentalDesk.DuckType&version=1.0.1 // Install MentalDesk.DuckType as a Cake Tool #tool nuget:?package=MentalDesk.DuckType&version=1.0.1
MentalDesk.DuckType
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.
Installation
To get started, add MentalDesk.DuckType
to your project by running the following command:
dotnet add package MentalDesk.DuckType
Usage
Imagine you have the following types:
class Duck
{
public void Quack() => Console.WriteLine("Quack!");
}
class Derek
{
public void Quack() => Console.WriteLine("quack...");
}
They 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
{
d.Quack();
}
Using MentalDesk.DuckType
you can add a couple of partial classes to your project like so:
public interface IDuck
{
public void Quack();
}
[DuckType<Duck, IDuck>]
partial class DuckDuck : IDuck
{
}
[DuckType<Derek, IDuck>]
partial class DerekDuck : IDuck
{
}
Each of the partial classes is decorated with a DuckType<TClass, TInterface>
attribute, which triggers 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)
{
d.Quack();
}
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 | Versions 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. |
-
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.