RSCG – Comparison

RSCG – Comparison
 
 

name Comparison
nuget https://www.nuget.org/packages/ReflectionIT.ComparisonOperatorsGenerator/
link https://github.com/sonnemaf/ReflectionIT.ComparisonOperatorsGenerator
author Fons Sonnemans

If you want to generate comparison operators for your classes, startin with IComparable

 

This is how you can use Comparison .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
	<ItemGroup>

		<PackageReference Include="ReflectionIT.ComparisonOperatorsGenerator" Version="0.1.2-preview" />
		
	</ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

</Project>


The code that you will use is


// See https://aka.ms/new-console-template for more information
using ComparisonDemo;

Console.WriteLine("Hello, World!");
var room = new Room
{
    Height = 10,
    Width = 20,
    Length = 30
};
var room2 = new Room
{
    Height = 15,
    Width = 25,
    Length = 35
};
Console.WriteLine($"Room Volume: {room.Volume}");
Console.WriteLine($"Room Comparison: {room.CompareTo(room2)}");
Console.WriteLine($"Room Comparison: {room < room2}");



&#91;/code&#93;
&#91;code lang="csharp"&#93;

using ReflectionIT.ComparisonOperatorsGenerator.Attributes;


namespace ComparisonDemo;
//https://github.com/sonnemaf/ReflectionIT.ComparisonOperatorsGenerator
&#91;ComparisonOperators&#93;
internal partial class Room : IComparable<Room>
{

    public int Height { get; set; }
    public int Width { get; set; }
    public int Length { get; set; }
    public int Volume => Height * Width * Length;

    public int CompareTo(Room? other)
    {
        return other is null ? 1 : Volume.CompareTo(other.Volume);
    }
}


 

The code that is generated is

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by the ReflectionIT.ComparisonOperatorsGenerator source generator
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace ComparisonDemo
{
    partial class Room
    {
        [global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.ComparisonOperatorsGenerator", "0.1.2.0")]
        [global::System.Diagnostics.DebuggerNonUserCode]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        /// <summary>Compares two values to determine which is less.</summary>
        /// <param name="left">The value to compare with <paramref name="right" />.</param>
        /// <param name="right">The value to compare with <paramref name="left" />.</param>
        /// <returns><c>true</c> if <paramref name="left" /> is less than <paramref name="right" />; otherwise, <c>false</c>.</returns>
        public static bool operator <(Room left, Room right) => left.CompareTo(right) < 0;
        
        &#91;global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.ComparisonOperatorsGenerator", "0.1.2.0")&#93;
        &#91;global::System.Diagnostics.DebuggerNonUserCode&#93;
        &#91;global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage&#93;
        /// <summary>Compares two values to determine which is less or equal.</summary>
        /// <param name="left">The value to compare with <paramref name="right" />.</param>
        /// <param name="right">The value to compare with <paramref name="left" />.</param>
        /// <returns><c>true</c> if <paramref name="left" /> is less than or equal to <paramref name="right" />; otherwise, <c>false</c>.</returns>
        public static bool operator <=(Room left, Room right) => left.CompareTo(right) <= 0;
        
        &#91;global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.ComparisonOperatorsGenerator", "0.1.2.0")&#93;
        &#91;global::System.Diagnostics.DebuggerNonUserCode&#93;
        &#91;global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage&#93;
        /// <summary>Compares two values to determine which is greater.</summary>
        /// <param name="left">The value to compare with <paramref name="right" />.</param>
        /// <param name="right">The value to compare with <paramref name="left" />.</param>
        /// <returns><c>true</c> if <paramref name="left" /> is greater than <paramref name="right" />; otherwise, <c>false</c>.</returns>
        public static bool operator >(Room left, Room right) => left.CompareTo(right) > 0;
        
        [global::System.CodeDom.Compiler.GeneratedCode("ReflectionIT.ComparisonOperatorsGenerator", "0.1.2.0")]
        [global::System.Diagnostics.DebuggerNonUserCode]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        /// <summary>Compares two values to determine which is greater or equal.</summary>
        /// <param name="left">The value to compare with <paramref name="right" />.</param>
        /// <param name="right">The value to compare with <paramref name="left" />.</param>
        /// <returns><c>true</c> if <paramref name="left" /> is greater than or equal to <paramref name="right" />; otherwise, <c>false</c>.</returns>
        public static bool operator >=(Room left, Room right) => left.CompareTo(right) >= 0;
        
    }
    
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by the ReflectionIT.ComparisonOperatorsGenerator source generator
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

#nullable enable
#if COMPARISON_OPERATORS_GENERATOR_EMBED_ATTRIBUTES

/// <summary>
/// An attribute to indicate that comparison operators should be generated for the target class, struct or record
/// <para>
/// This only works if the <see cref="System.IComparable{T}"/> interface is implemented
/// </para>
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
public class ComparisonOperatorsAttribute : Attribute {

    /// <summary>
    /// Gets or sets a value indicating whether the <see cref="System.Numerics.IComparisonOperators{TSelf,TOther,TResult}" /> interface should be implemented.
    /// </summary>
    public bool ImplementIComparisonOperatorsInterface { get; set; }
}
#endif

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Comparison


Posted

in

,

by

Tags: