ReflectionIT.ComparisonOperatorsGenerator 0.1.2-preview

Prefix Reserved
This is a prerelease version of ReflectionIT.ComparisonOperatorsGenerator.
dotnet add package ReflectionIT.ComparisonOperatorsGenerator --version 0.1.2-preview
                    
NuGet\Install-Package ReflectionIT.ComparisonOperatorsGenerator -Version 0.1.2-preview
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ReflectionIT.ComparisonOperatorsGenerator" Version="0.1.2-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ReflectionIT.ComparisonOperatorsGenerator" Version="0.1.2-preview" />
                    
Directory.Packages.props
<PackageReference Include="ReflectionIT.ComparisonOperatorsGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ReflectionIT.ComparisonOperatorsGenerator --version 0.1.2-preview
                    
#r "nuget: ReflectionIT.ComparisonOperatorsGenerator, 0.1.2-preview"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=ReflectionIT.ComparisonOperatorsGenerator&version=0.1.2-preview&prerelease
                    
Install ReflectionIT.ComparisonOperatorsGenerator as a Cake Addin
#tool nuget:?package=ReflectionIT.ComparisonOperatorsGenerator&version=0.1.2-preview&prerelease
                    
Install ReflectionIT.ComparisonOperatorsGenerator as a Cake Tool

ReflectionIT.ComparisonOperatorsGenerator

A Source Generator package that generates the >, >=, <, <= operators for a partial type (class, struct or record) which implements IComparable<T>.

Generating these additional operators is as simple as adding the ComparisonOperators attribute to your type. Make sure this type is partial and implements System.IComparable<T>

NuGet packages

Package Version
ReflectionIT.ComparisonOperatorsGenerator NuGet

Example

Add the NuGet package and write the following code:

using ReflectionIT.ComparisonOperatorsGenerator.Attributes;

[ComparisonOperators]
partial class Point : IComparable<Point> {

    public readonly double X;
    public readonly double Y;

    public Point(double x, double y) {
        this.X = x;
        this.Y = y;
    }

    public void Swap() => new Point(this.Y, this.X);

    public double Dist => Math.Sqrt((X * X) + (Y * Y));

    public override string ToString() => $"({X},{Y})";

    public int CompareTo(Point? other) {
        return Comparer<double?>.Default.Compare(this.Dist, other?.Dist);
    }
}

This will generate the following partial class with the 4 comparison operators.

partial class Point : System.Numerics.IComparisonOperators<Point,Point,bool> 
{
    public static bool operator <(Point left, Point right) => left.CompareTo(right) < 0;
        
    public static bool operator <=(Point left, Point right) => left.CompareTo(right) <= 0;
        
    public static bool operator >(Point left, Point right) => left.CompareTo(right) > 0;
        
    public static bool operator >=(Point left, Point right) => left.CompareTo(right) >= 0;       
}

Implement IComparisonOperators<TSelf,TOther,TResult> interface

You can automatically implement the IComparisonOperators<TSelf,TOther,TResult> interface using the ImplementIComparisonOperatorsInterface property of the ComparisonOperators attribute.

using ReflectionIT.ComparisonOperatorsGenerator.Attributes;

[ComparisonOperators(ImplementIComparisonOperatorsInterface = true)]
readonly partial record struct Time : IComparable<Time> {

    public readonly int TotalMinutes; 

    public int Hours => TotalMinutes / 60;
    public int Minutes => TotalMinutes % 60;
    public Time(int totalMinutes) {
        ArgumentOutOfRangeException.ThrowIfNegative(totalMinutes);
        TotalMinutes = totalMinutes;
    }

    public Time(int hours, int minutes) : this(hours * 60 + minutes) {
    }

    public override string ToString() => $"{this.Hours}:{this.Minutes:00}";

    public int CompareTo(Time other) => this.TotalMinutes.CompareTo(other.TotalMinutes);
}

This will generate the following partial record struct with the 4 comparison operators and the IComparisonOperators<TSelf,TOther,TResult> interface implementation

partial record struct Time : global::System.Numerics.IComparisonOperators<Time,Time,bool> 
{
    public static bool operator <(Time left, Time right) => left.CompareTo(right) < 0;
        
    public static bool operator <=(Time left, Time right) => left.CompareTo(right) <= 0;
        
    public static bool operator >(Time left, Time right) => left.CompareTo(right) > 0;
        
    public static bool operator >=(Time left, Time right) => left.CompareTo(right) >= 0;
        
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.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.

Version Downloads Last updated
0.1.2-preview 89 4/11/2025
0.1.1-preview 120 4/9/2025
0.1.0-preview 116 4/9/2025

Namespace added to the generated file name, fixes duplicate typename issues