EMDD.KtEquatable 3.0.0

.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
Install-Package EMDD.KtEquatable -Version 3.0.0
dotnet add package EMDD.KtEquatable --version 3.0.0
<PackageReference Include="EMDD.KtEquatable" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EMDD.KtEquatable --version 3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: EMDD.KtEquatable, 3.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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=3.0.0

// Install EMDD.KtEquatable as a Cake Tool
#tool nuget:?package=EMDD.KtEquatable&version=3.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

  NugetNugetGitHub Workflow Status  

EMDD.KtEquatable

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


Requirements

Visual Studio 16.8 or greater

.Net 5.0.102 sdk or greater

Nuget Package Usage

https://www.nuget.org/packages/EMDD.KtEquatable/

<PackageReference Include="EMDD.KtEquatable" Version="*.*.*" />

Breaking Changes and Updates

(2.0.2 to 3.0.0)

  • ditched EMDD.KtEquatable.Core, it's not needed any longer.
    • The <IncludeBuildOutput>false</IncludeBuildOutput> has to be removed in order for the custom attributes to be visible for use, this way the project is treated both as an analyzer and a common library. The only problem is that if a project using this source generator is referenced by another project, this source generator will also be visible on the referencing project.
  • Added Diagnostic Reports (Compilation Error Reports)
  • Changes on the Attribute usage particularly FloatingPointEqualityAttribute
    • the use of the precision parameter FloatingPointEqualityAttribute
    • Enumerable attributes can be combined with ReferenceEqualityAttribute and FloatingPointEqualityAttribute to produce combined effect
    • UnorderedEquality, OrderedEquality, and SetEquality was replaced with EnumerableEquality with parameters
  • Improvement on the indentions of the autogenerated codes
  • DoubleEnumerableEqualityAttribute, has been removed

see History of Breaking Changes and Updates

Usage

The source generator can be used by marking the target class/record 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(4)]
    public double Salary { get; set; }

    [EnumerableEquality(EnumerableOrderType.Unordered)]
    public Dictionary<string, int>? BankAccountDetails { get; set; }

    [EnumerableEquality(EnumerableOrderType.Ordered)]
    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);
    }
}

note: the class marked with [Equatable] including its parent/containing classes must be marked as partial

Supported Attributes

Class/Record Attributes

Equatable

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

Todo

Implementation for struct (if soon be required)

Property Attributes

Default (No Attribute)

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(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 compared using the build-in EqualityComparer:

FloatingPointEqualityComparer
ReferenceEquality

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

[ReferenceEquality]
public Employee Superior { get; set; }
EnumerableEquality

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 
{
    [EnumerableEquality(EnumerableOrderType.Ordered)]
    public DateTime[] Borrower { get; set; } 

    [EnumerableEquality(EnumerableOrderType.Unordered)]
    public string[] BookTitle { get; set; } 

    [EnumerableEquality(EnumerableOrderType.Set)]
    public HashSet<string> Borrowers { get; set; }
}

ReferenceEquality and FloatingPointEquality can also be mixed with EnumerableEquality. Say if you want to compare an List of unordered double property with 3 decimal point precisions:

[Equatable]
partial class Mechanic
{
    [EnumerableEquality(EnumerableOrderType.Ordered)]
    [FLoatingPointEquality(3)]
    public System.List<double> Payments { get; set; } 
}

The Payments property will be compared using new UnorderedEqualityComparer(new FloatingPointEqualityComparer(3))

Diagnostic Reports

Compile-time Diagnostic reports were added at 3.0.0. see Diagnostic Lists

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
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 170 8/21/2021
3.2.2 152 5/28/2021
3.2.0 119 5/28/2021
3.1.0.1-alpha 111 5/28/2021
3.1.0 149 5/25/2021
3.0.0 156 5/21/2021
2.0.2 124 5/2/2021
2.0.1 145 5/2/2021

Adding Method for IEnumerable<double>