EMDD.KtSourceGen.KtEquatable
1.0.0.1
Breaking Changes on the namespace usage.
Breaking Changes on how the attributes are written and implemented
dotnet add package EMDD.KtSourceGen.KtEquatable --version 1.0.0.1
NuGet\Install-Package EMDD.KtSourceGen.KtEquatable -Version 1.0.0.1
<PackageReference Include="EMDD.KtSourceGen.KtEquatable" Version="1.0.0.1" />
paket add EMDD.KtSourceGen.KtEquatable --version 1.0.0.1
#r "nuget: EMDD.KtSourceGen.KtEquatable, 1.0.0.1"
// Install EMDD.KtSourceGen.KtEquatable as a Cake Addin
#addin nuget:?package=EMDD.KtSourceGen.KtEquatable&version=1.0.0.1
// Install EMDD.KtSourceGen.KtEquatable as a Cake Tool
#tool nuget:?package=EMDD.KtSourceGen.KtEquatable&version=1.0.0.1
EMDD.KtEquatable
use of C# 9.0 Source Generator to AutoGenerate IEquatable<T> using attributes.
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.KtSourceGen.KtEquatable.Core;
[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]
.
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; }
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. |
.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
- Microsoft.Bcl.HashCode (>= 1.1.0)
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 |
---|
Updated the github reference