EMDD.KtEquatable 2.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package EMDD.KtEquatable --version 2.0.1
NuGet\Install-Package EMDD.KtEquatable -Version 2.0.1
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="EMDD.KtEquatable" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EMDD.KtEquatable --version 2.0.1
#r "nuget: EMDD.KtEquatable, 2.0.1"
#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.
// Install EMDD.KtEquatable as a Cake Addin
#addin nuget:?package=EMDD.KtEquatable&version=2.0.1

// Install EMDD.KtEquatable as a Cake Tool
#tool nuget:?package=EMDD.KtEquatable&version=2.0.1

Nuget

EMDD.KtEquatable

use of C# 9.0 Source Generator to AutoGenerate IEquatable<T> using attributes.


Nuget Package Usage

Install-Package EMDD.KtEquatable -Version 2.0.1

this will also require the installation of

Install-Package EMDD.KtEquatable.Core -Version 3.0.0

Breaking Changes and Updates (1.0.0 to 2.0.1)

  • A major breaking change was implemented from 1.0.0 to 2.0.1. The original namespace used to access the Attributes was changed from EMDD.KtSourceGen.KtEquatable.Core to EMDD.KtEquatable.Core.Attributes
  • Implementation improvement for the checking of base class was also introduced. If the base class that the class marked with [Equatable] Attribute was derived from implements IEnumerable<T> or is marked with [Equatable] Attribute itself, the implementation of the Equals checking and GetHashCode will pick-up the Equals and GetHashCode implementation of the base class, but for base classes that does not derive IEquatable<T> or has no [Equatable], the Equals and GetHashCode implementation bypass the base class, instead, the Equals and GetHashCode will use property of the base class + it's own properties in the computation.
  • The Attributes and custom EqualityComparers are now placed under EMDD.KtEquatable.Core package, instead of being autogenerated. This will eliminate conflicting codes if two projects uses this package.

Usage

The source generator can be used by marking the target class with [Equatable] Attribute. The properties can also be marked with specific attributes specify the equality comparison method to be used. The sample below shows an EmployeeInfo class marked with the specific Attributes.

using EMDD.KtEquatable.Core.Attributes;

[Equatable]
partial class EmployeeInfo
{
    public string? Name { get; set; }
    
    [IgnoreEquality]
    public int Id { get; set; }

    [FloatingPointEquality(Precision = 4)]
    public double Salary { get; set; }

    [UnorderedEquality]
    public Dictionary<string, int>? BankAccountDetails { get; set; }

    [OrderedEquality]
    public List<DateTime>? TimeIn { get; set; }

    [ReferenceEquality]
    public EmployeeInfo? Superior { get; set; }

    [ReferenceEquality]
    public string? SocialSecurity { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        const string SS = "BBB";
        
        EmployeeInfo boss1 = new EmployeeInfo() { Name = "Bob", Id = 1 };
        EmployeeInfo boss2 = new EmployeeInfo() { Name = "Bob", Id = 2 };
        EmployeeInfo employee1 = new EmployeeInfo() {
            Name = "Chipotle",
            Id = 3,
            Salary = 10000.0003,
            BankAccountDetails = new Dictionary<string, int> { { "Wells", 123 }, { "JP", 234 }, { "BoA", 345 } },
            Superior = boss1,
            TimeIn= new List<DateTime> { new DateTime (2012,3,4), new DateTime(2012, 3, 5), new DateTime(2012, 3, 6) },
            SocialSecurity="AAA"
        };
        EmployeeInfo employee2 = new EmployeeInfo() {
            Name = "Chipotle",
            Id = 3,
            Salary = 10000.0003,
            BankAccountDetails = new Dictionary<string, int> { { "Wells", 123 }, { "JP", 234 }, { "BoA", 345 } },
            Superior = boss2,
            TimeIn= new List<DateTime> { new DateTime (2012,3,4), new DateTime(2012, 3, 5), new DateTime(2012, 3, 6)},
            SocialSecurity= SS
        };
        EmployeeInfo employee3 = new EmployeeInfo() {
            Name = "Chipotle",
            Id = 3,
            Salary = 10000.0004,
            BankAccountDetails = new Dictionary<string, int> { { "Wells", 123 }, { "JP", 234 }, { "BoA", 345 } },
            Superior = boss2,
            TimeIn= new List<DateTime> { new DateTime (2012,3,4), new DateTime(2012, 3, 5), new DateTime(2012, 3, 6) },
            SocialSecurity = SS
        };
        Console.WriteLine(employee1 != employee2);
        Console.WriteLine(employee2 == employee3);
    }
}

Supported Attributes

Equatable

The code generator will only recognize class orrecordmarked with [Equatable].

Default

A property that is not marked by any Attributes mentioned below will produce a generated code that uses EqualityComparer<T>.Default when checking Equality and calculating Hashcode.

IgnoreEquality

Properties marked with [IgnoreEquality] will not be included in the equality checking and Hashcode calculation

[IgnoreEquality] 
public string Name { get; set; }

FloatingPointEquality

[FloatingPointEquality(Precision = 10)]
public double Salary { get; set; } // Must be double

A property marked with [FloatingPointEquality] will be used in the comparison of equality such that the difference between the compared value should not be less than the precision. the property will be equal if:

System.Math.Abs(_value1.Salary - _value2.Salary) < System.Math.Pow(10,-Precision)

The hashcode is also computed by rounding off the value of the property

System.Math.Round(Salary * System.Math.Pow(10,Precision), 0).GetHashCode();

OrderedEquality, UnorderedEquality, and SetEquality

Comparison of Collections/IEnumerables with specific requirements such as when the order is not or is required or if the collection can have repeated elements.

[Equatable]
partial class Book 
{
    [OrderedEquality] 
    public DateTime[] Borrower { get; set; } 

    [UnorderedEquality] 
    public string[] BookTitle { get; set; } 

    [SetEquality] 
    public HashSet<string> Borrowers { get; set; }
}

ReferenceEquality

A property marked with [ReferenceEquality] will use Reference equality checking only

[ReferenceEquality]
public Employee Superior { get; set; }
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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
3.3.1 374 8/21/2021
3.2.2 347 5/28/2021
3.2.0 312 5/28/2021
3.1.0.1-alpha 208 5/28/2021
3.1.0 315 5/25/2021
3.0.0 325 5/21/2021
2.0.2 315 5/2/2021
2.0.1 318 5/2/2021

Breaking Changes!
Namespace changes
Changes onBClassREreaking Changes!,