EMDD.KtEquatable
2.0.2
See the version list below for details.
dotnet add package EMDD.KtEquatable --version 2.0.2
NuGet\Install-Package EMDD.KtEquatable -Version 2.0.2
<PackageReference Include="EMDD.KtEquatable" Version="2.0.2" />
paket add EMDD.KtEquatable --version 2.0.2
#r "nuget: EMDD.KtEquatable, 2.0.2"
// Install EMDD.KtEquatable as a Cake Addin #addin nuget:?package=EMDD.KtEquatable&version=2.0.2 // Install EMDD.KtEquatable as a Cake Tool #tool nuget:?package=EMDD.KtEquatable&version=2.0.2
EMDD.KtEquatable
use of C# 9.0 Source Generator to AutoGenerate IEquatable<T> using attributes.
Nuget Package Usage
Install-Package EMDD.KtEquatable -Version x.x.x
this will also require the installation of
Install-Package EMDD.KtEquatable.Core -Version x.x.x
Breaking Changes and Updates
(2.0.1 to 2.0.2)
- Added
[DoubleEnumerableEqualityAttribute]
(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
toEMDD.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 implementsIEnumerable<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 deriveIEquatable<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
orrecord
marked with [Equatable]
.
Todo
Implementation for struct (if soon be required)
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();
DoubleEnumerableEquality (2.0.2)
[DoubleEnumerableEquality(Precision = 10, Ordered = true, IsSet = false)]
public List<double> Salary { get; set; } // Must be double
Can be apploed to property whose type implements IEnumerable<double>
.
Usage
Set Precision
to limit the numbers of decimal places to check
Set Ordered
true if order is important, false if order does not matter
Set IsSet
true if the IEnumerable is a set type collection. Ordered
will be bypassed if IsSet
is true
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; }
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- EMDD.KtEquatable.Core (>= 3.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Adding Method for IEnumerable<double>