PolyType 0.21.1
See the version list below for details.
dotnet add package PolyType --version 0.21.1
NuGet\Install-Package PolyType -Version 0.21.1
<PackageReference Include="PolyType" Version="0.21.1" />
paket add PolyType --version 0.21.1
#r "nuget: PolyType, 0.21.1"
// Install PolyType as a Cake Addin #addin nuget:?package=PolyType&version=0.21.1 // Install PolyType as a Cake Tool #tool nuget:?package=PolyType&version=0.21.1
PolyType
PolyType is a practical generic programming library for .NET. It facilitates the rapid development of feature-complete, high-performance libraries that interact with user-defined types. This includes serializers, structured loggers, mappers, validators, parsers, random generators, and equality comparers. Its built-in source generator ensures that any library built on top of PolyType gets Native AOT support for free.
The project is a port of the TypeShape library for F#, adapted to patterns and idioms available in C#. The name PolyType is a reference to polytypic programming, another term for generic programming.
See the project website for additional background and API documentation.
Quick Start
You can try the library by installing the PolyType
NuGet package:
$ dotnet add package PolyType
which includes the core types and source generator for generating type shapes:
using PolyType;
[GenerateShape]
public partial record Person(string name, int age);
Doing this will augment Person
with an implementation of the IShapeable<Person>
interface. This suffices to make Person
usable with any library that targets the PolyType core abstractions. You can try this out by installing the built-in example libraries:
$ dotnet add package PolyType.Examples
Here's how the same value can be serialized to three separate formats.
using PolyType.Examples.JsonSerializer;
using PolyType.Examples.CborSerializer;
using PolyType.Examples.XmlSerializer;
Person person = new("Pete", 70);
JsonSerializerTS.Serialize(person); // {"Name":"Pete","Age":70}
XmlSerializer.Serialize(person); // <value><Name>Pete</Name><Age>70</Age></value>
CborSerializer.EncodeToHex(person); // A2644E616D656450657465634167651846
Since the application uses a source generator to produce the shape for Person
, it is fully compatible with Native AOT. See the shape providers article for more details on how to use the library with your types.
Authoring PolyType Libraries
As a library author, PolyType makes it easy to write high-performance, feature-complete components by targeting its core abstractions. For example, a parser API using PolyType might look as follows:
public static class MyFancyParser
{
public static T? Parse<T>(string myFancyFormat) where T : IShapeable<T>;
}
The IShapeable<T>
constraint indicates that the parser only works with types augmented with PolyType metadata. This metadata can be provided using the PolyType source generator:
Person? person = MyFancyParser.Parse<Person>(format); // Compiles
[GenerateShape] // Generate an IShapeable<TPerson> implementation
partial record Person(string name, int age, List<Person> children);
For more information see:
- The core abstractions document for an overview of the core programming model.
- The shape providers document for an overview of the built-in shape providers and their APIs.
- The generated API documentation for the project.
- The
PolyType.Examples
project for advanced examples of libraries built on top of PolyType.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- No dependencies.
NuGet packages (3)
Showing the top 3 NuGet packages that depend on PolyType:
Package | Downloads |
---|---|
PolyType.TestCases
Practical generic programming for C# |
|
PolyType.Examples
Practical generic programming for C# |
|
Nerdbank.MessagePack
A fast and more user-friendly MessagePack serialization library for .NET. With ground-up support for trimming and Native AOT. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.23.1 | 617 | 12/12/2024 |
0.22.1 | 325 | 12/5/2024 |
0.21.1 | 232 | 11/26/2024 |
0.20.1 | 195 | 11/23/2024 |
0.19.3 | 104 | 11/19/2024 |
0.19.2 | 94 | 11/19/2024 |
0.19.1 | 93 | 11/19/2024 |
0.18.2 | 95 | 11/18/2024 |
0.18.1 | 162 | 11/17/2024 |
0.17.1 | 149 | 11/16/2024 |
0.16.10 | 105 | 11/15/2024 |
0.16.1 | 255 | 11/8/2024 |
0.15.3-gdec65ec2a5 | 97 | 11/7/2024 |