ReflectionIT.ComparisonOperatorsGenerator
1.1.2
Prefix Reserved
dotnet add package ReflectionIT.ComparisonOperatorsGenerator --version 1.1.2
NuGet\Install-Package ReflectionIT.ComparisonOperatorsGenerator -Version 1.1.2
<PackageReference Include="ReflectionIT.ComparisonOperatorsGenerator" Version="1.1.2" />
<PackageVersion Include="ReflectionIT.ComparisonOperatorsGenerator" Version="1.1.2" />
<PackageReference Include="ReflectionIT.ComparisonOperatorsGenerator" />
paket add ReflectionIT.ComparisonOperatorsGenerator --version 1.1.2
#r "nuget: ReflectionIT.ComparisonOperatorsGenerator, 1.1.2"
#:package ReflectionIT.ComparisonOperatorsGenerator@1.1.2
#addin nuget:?package=ReflectionIT.ComparisonOperatorsGenerator&version=1.1.2
#tool nuget:?package=ReflectionIT.ComparisonOperatorsGenerator&version=1.1.2
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 |
Example
Add the NuGet package and write the following code:
using ReflectionIT.ComparisonOperatorsGenerator.Attributes;
[ComparisonOperators]
partial class Point : IComparable<Point> {
public double X { get; }
public double Y { get; }
public Point(double x, double y) {
this.X = x;
this.Y = y;
}
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 | Versions 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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. |
-
.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 |
|---|---|---|
| 1.1.2 | 482 | 12/1/2025 |
| 1.1.1 | 118 | 11/28/2025 |
| 1.1.0 | 114 | 11/28/2025 |
| 1.0.1 | 234 | 8/29/2025 |
| 1.0.0 | 175 | 8/14/2025 |
| 0.1.2-preview | 196 | 4/11/2025 |
| 0.1.1-preview | 189 | 4/9/2025 |
| 0.1.0-preview | 188 | 4/9/2025 |
Improved generated file naming for generic types.