SkillAgentSearch skills...

UnitGenerator

C# Source Generator to create value-object, inspired by units of measure.

Install / Use

/learn @Cysharp/UnitGenerator
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

UnitGenerator

GitHub Actions Releases

C# Source Generator to create Value object pattern, also inspired by units of measure to support all arithmetic operators and serialization.

NuGet: UnitGenerator

Install-Package UnitGenerator

Execute in Unity Game Engine is also supported, please see the Unity section for details.

Introduction

For example, Identifier, UserId is comparable only to UserId, and cannot be assigned to any other type. Also, arithmetic operations are not allowed.

using UnitGenerator;

[UnitOf(typeof(int))]
public readonly partial struct UserId; { }

or when using C#11 and NET7 you can use

using UnitGenerator;

[UnitOf<int>] public readonly partial struct UserId;

will generates

[System.ComponentModel.TypeConverter(typeof(UserIdTypeConverter))]
public readonly partial struct UserId : IEquatable<UserId> 
{
    readonly int value;
    
    public UserId(int value)
    {
        this.value = value;
    }

    public readonly int AsPrimitive() => value;
    public static explicit operator int(UserId value) => value.value;
    public static explicit operator UserId(int value) => new UserId(value);
    public bool Equals(UserId other) => value.Equals(other.value);
    public override bool Equals(object? obj) => // snip...
    public override int GetHashCode() => value.GetHashCode();
    public override string ToString() => value.ToString();
    public static bool operator ==(in UserId x, in UserId y) => x.value.Equals(y.value);
    public static bool operator !=(in UserId x, in UserId y) => !x.value.Equals(y.value);

    private class UserIdTypeConverter : System.ComponentModel.TypeConverter
    {
        // snip...
    }
}

However, Hp in games, should not be allowed to be assigned to other types, but should support arithmetic operations with int. For example double heal = target.Hp = Hp.Min(target.Hp * 2, target.MaxHp).

[UnitOf<int>(UnitGenerateOptions.ArithmeticOperator | UnitGenerateOptions.ValueArithmeticOperator | UnitGenerateOptions.Comparable | UnitGenerateOptions.MinMaxMethod)]
public readonly partial struct Hp;

// -- generates

[System.ComponentModel.TypeConverter(typeof(HpTypeConverter))]
public readonly partial struct Hp
    : IEquatable<Hp>
#if NET7_0_OR_GREATER
    , IEqualityOperators<Hp, Hp, bool>
#endif    
    , IComparable<Hp>
#if NET7_0_OR_GREATER
    , IComparisonOperators<Hp, Hp, bool>
#endif
#if NET7_0_OR_GREATER
    , IAdditionOperators<Hp, Hp, Hp>
    , ISubtractionOperators<Hp, Hp, Hp>
    , IMultiplyOperators<Hp, Hp, Hp>
    , IDivisionOperators<Hp, Hp, Hp>
    , IUnaryPlusOperators<Hp, Hp>
    , IUnaryNegationOperators<Hp, Hp>
    , IIncrementOperators<Hp>
    , IDecrementOperators<Hp>
#endif    
{
    readonly int value;

    public Hp(int value)
    {
        this.value = value;
    }

    public int AsPrimitive() => value;
    public static explicit operator int(Hp value) => value.value;
    public static explicit operator Hp(int value) => new Hp(value);
    public bool Equals(Hp other) => value.Equals(other.value);
    public override bool Equals(object? obj) => // snip...
    public override int GetHashCode() => value.GetHashCode();
    public override string ToString() => value.ToString();
    public static bool operator ==(in Hp x, in Hp y) => x.value.Equals(y.value);
    public static bool operator !=(in Hp x, in Hp y) => !x.value.Equals(y.value);
    private class HpTypeConverter : System.ComponentModel.TypeConverter { /* snip... */ }

    // UnitGenerateOptions.ArithmeticOperator
    public static Hp operator +(Hp x, Hp y) => new Hp(checked((int)(x.value + y.value)));
    public static Hp operator -(Hp x, Hp y) => new Hp(checked((int)(x.value - y.value)));
    public static Hp operator *(Hp x, Hp y) => new Hp(checked((int)(x.value * y.value)));
    public static Hp operator /(Hp x, Hp y) => new Hp(checked((int)(x.value / y.value)));
    public static Hp operator ++(Hp x) => new Hp(checked((int)(x.value + 1)));
    public static Hp operator --(Hp x) => new Hp(checked((int)(x.value - 1)));
    public static Hp operator +(A value) => new((int)(+value.value));
    public static Hp operator -(A value) => new((int)(-value.value));

    // UnitGenerateOptions.ValueArithmeticOperator
    public static Hp operator +(Hp x, in int y) => new Hp(checked((int)(x.value + y)));
    public static Hp operator -(Hp x, in int y) => new Hp(checked((int)(x.value - y)));
    public static Hp operator *(Hp x, in int y) => new Hp(checked((int)(x.value * y)));
    public static Hp operator /(Hp x, in int y) => new Hp(checked((int)(x.value / y)));

    // UnitGenerateOptions.Comparable
    public int CompareTo(Hp other) => value.CompareTo(other.value);
    public static bool operator >(Hp x, Hp y) => x.value > y.value;
    public static bool operator <(Hp x, Hp y) => x.value < y.value;
    public static bool operator >=(Hp x, Hp y) => x.value >= y.value;
    public static bool operator <=(Hp x, Hp y) => x.value <= y.value;

    // UnitGenerateOptions.MinMaxMethod
    public static Hp Min(Hp x, Hp y) => new Hp(Math.Min(x.value, y.value));
    public static Hp Max(Hp x, Hp y) => new Hp(Math.Max(x.value, y.value));
}

You can configure with UnitGenerateOptions, which method to implement.

[Flags]
enum UnitGenerateOptions
{
    None = 0,
    ImplicitOperator = 1,
    ParseMethod = 1 << 1,
    MinMaxMethod = 1 << 2,
    ArithmeticOperator = 1 << 3,
    ValueArithmeticOperator = 1 << 4,
    Comparable = 1 << 5,
    Validate = 1 << 6,
    JsonConverter = 1 << 7,
    MessagePackFormatter = 1 << 8,
    DapperTypeHandler = 1 << 9,
    EntityFrameworkValueConverter = 1 << 10,
    WithoutComparisonOperator = 1 << 11,
    JsonConverterDictionaryKeySupport = 1 << 12,
    Normalize = 1 << 13,
}

UnitGenerateOptions has some serializer support. For example, a result like Serialize(userId) => { Value = 1111 } is awful. The value-object should be serialized natively, i.e. Serialize(useId) => 1111, and should be able to be added directly to a database, etc.

Currently UnitGenerator supports MessagePack for C#, System.Text.Json(JsonSerializer), Dapper and EntityFrameworkCore.

[UnitOf<int>(UnitGenerateOptions.MessagePackFormatter)]
public readonly partial struct UserId;

// -- generates

[MessagePackFormatter(typeof(UserIdMessagePackFormatter))]
public readonly partial struct UserId 
{
    class UserIdMessagePackFormatter : IMessagePackFormatter<UserId>
    {
        public void Serialize(ref MessagePackWriter writer, UserId value, MessagePackSerializerOptions options)
        {
            options.Resolver.GetFormatterWithVerify<int>().Serialize(ref writer, value.value, options);
        }

        public UserId Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            return new UserId(options.Resolver.GetFormatterWithVerify<int>().Deserialize(ref reader, options));
        }
    }
}
<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

Table of Contents

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

UnitOfAttribute

When referring to the UnitGenerator, it generates a internal UnitOfAttribute.

namespace UnitGenerator
{
    [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
    internal class UnitOfAttribute : Attribute
    {
        public Type Type { get; }
        public UnitGenerateOptions Options { get; }
        public UnitArithmeticOperators ArithmeticOperators { get; set; }
        public string? ToStringFormat { get; set; }
        
        public UnitOfAttribute(Type type, UnitGenerateOptions options = UnitGenerateOptions.None) { ... }
    }

#if NET7_0_OR_GREATER
    [AttributeUsage(AttributeTargets.Struct, AllowMultiple = false)]
    internal class UnitOfAttribute<T> : Attribute
    {
        public Type Type { get; }
        public UnitGenerateOptions Options { get; }
        public UnitArithmeticOperators ArithmeticOperators { get; set; } = UnitArithmeticOperators.All;
        public string? ToStringFormat { get; set; }

        public UnitOfAttribute(UnitGenerateOptions options = UnitGenerateOptions.None)
        {
            this.Type = typeof(T);
            this.Options = options;
        }
    }
#endif
}

You can attach this attribute with any specified underlying type to readonly partial struct.

[UnitOf(typeof(Guid))]
public readonly partial struct GroupId { }

[UnitOf(typeof(string))]
public readonly partial struct Message { }

[UnitOf(typeof(long))]
public re
View on GitHub
GitHub Stars396
CategoryDevelopment
Updated2d ago
Forks22

Languages

C#

Security Score

95/100

Audited on Mar 22, 2026

No findings