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, RPC frameworks, 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.
The following projects have been based on PolyType:
- Nerdbank.MessagePack - a MessagePack library with performance to rival MessagePack-CSharp, and greater simplicity and additional features.
- “At Alvys, we built an EDI mapping engine to automate and accelerate EDI integrations. The engine had to be fast, configurable and portable across EDI versions. A crucial step is mapping the EDI segments model to an object model. For this, we used PolyType, which allowed us to easily build both the encoder and the decoder. We appreciated the emphasis on fundamentals and performance, as well as the plethora of examples. The result: a solution with only a fraction of the code and an order-of-magnitude increase in micro-benchmark performance compared to the previous solution that used a commercial EDI library.” — Leo Gorodinski
The repo consists of the following projects:
- The core
PolyTypelibrary containing:- The core abstractions defining the type model.
- The reflection provider implementation.
- The model classes used by the source generator.
- The
PolyType.SourceGeneratorproject contains the built-in source generator implementation. - The
PolyType.Roslynlibrary exposes a set of components for extracting data models from Roslyn type symbols. Used as the foundation for the built-in source generator. PolyType.Examplescontaining library examples:- A serializer built on top of System.Text.Json,
- A serializer built on top of System.Xml,
- A serializer built on top of System.Formats.Cbor,
- A
ConfigurationBinderlike implementation, - A dependency injection implementation,
- A simple pretty-printer for .NET values,
- A generic random value generator based on
System.Random, - A JSON schema generator for .NET types,
- An object cloning function,
- A structural
IEqualityComparer<T>generator for POCOs and collections, - An object validator in the style of System.ComponentModel.DataAnnotations.
- A simple .NET object mapper.
- The
applicationsfolder contains sample Native AOT console applications.
CI builds of NuGet packages are available on feedz.io. To use the feed, add the following package source to your NuGet.config:
<configuration>
<packageSources>
<add key="feedz.io" value="https://f.feedz.io/eiriktsarpalis/PolyType/nuget/index.json" />
</packageSources>