Table of Contents

Getting Started

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:

string myFancyFormat = "..."; // Some format string
Person? person = MyFancyParser.Parse<Person>(myFancyFormat); // Compiles

[GenerateShape] // Generate an IShapeable<Person> implementation
partial record Person(string name, int age, List<Person> children);

For more information, see: