Skip to content

Commit b89eaf6

Browse files
committed
update docs
1 parent b40d593 commit b89eaf6

10 files changed

+77
-6
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ This library requires:
4141

4242
Place equatable attribute on a `class`, `record` or `struct`. The source generate will create a partial with overrides for `Equals` and `GetHashCode`.
4343

44-
- `[Equatable]` Marks the class to generate overrides for `Equals` and `GetHashCod`
44+
- `[Equatable]` Marks the class to generate overrides for `Equals` and `GetHashCode`
4545

4646
The default comparer used in the implementation of `Equals` and `GetHashCode` is `EqualityComparer<T>.Default`. Customize the comparer used with the following attributes.
4747

@@ -52,3 +52,38 @@ Place equatable attribute on a `class`, `record` or `struct`. The source genera
5252
- `[HashSetEquality]` Use `ISet<T>.SetEquals` to determine whether enumerables are equal
5353
- `[ReferenceEquality]` Use `Object.ReferenceEquals` to determines whether instances are the same instance
5454
- `[EqualityComparer]` Use the specified `EqualityComparer`
55+
56+
### Example Usage
57+
58+
Example of using the attributes to customize the source generation of `Equals` and `GetHashCode`
59+
60+
``` c#
61+
[Equatable]
62+
public partial class UserImport
63+
{
64+
[StringEquality(StringComparison.OrdinalIgnoreCase)]
65+
public string EmailAddress { get; set; } = null!;
66+
67+
public string? DisplayName { get; set; }
68+
69+
public string? FirstName { get; set; }
70+
71+
public string? LastName { get; set; }
72+
73+
public DateTimeOffset? LockoutEnd { get; set; }
74+
75+
public DateTimeOffset? LastLogin { get; set; }
76+
77+
[IgnoreEquality]
78+
public string FullName => $"{FirstName} {LastName}";
79+
80+
[HashSetEquality]
81+
public HashSet<string>? Roles { get; set; }
82+
83+
[DictionaryEquality]
84+
public Dictionary<string, int>? Permissions { get; set; }
85+
86+
[SequenceEquality]
87+
public List<DateTimeOffset>? History { get; set; }
88+
}
89+
```

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Authors>LoreSoft</Authors>
77
<NeutralLanguage>en-US</NeutralLanguage>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
9-
<PackageTags>dotnet roslyn source-generator generator</PackageTags>
9+
<PackageTags>dotnet roslyn source-generator generator equality equals equatable hashcode</PackageTags>
1010
<PackageProjectUrl>https://github.com/loresoft/Equatable.Generator</PackageProjectUrl>
1111
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1212
<PackageIcon>logo.png</PackageIcon>

src/Equatable.Generator/Attributes/DictionaryEqualityAttribute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Equatable.Attributes;
44

5+
/// <summary>
6+
/// Use a dictionary based comparer to determine if dictionaries are equal
7+
/// </summary>
58
[Conditional("EQUATABLE_GENERATOR")]
69
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
710
public class DictionaryEqualityAttribute : Attribute;

src/Equatable.Generator/Attributes/EqualityComparerAttribute.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
namespace Equatable.Attributes;
44

5+
/// <summary>
6+
/// Use the specified <see cref="IEqualityComparer{T}"/> in Equals and GetHashCode implementations
7+
/// </summary>
8+
/// <param name="equalityType">The <see cref="IEqualityComparer{T}"/> type to use</param>
9+
/// <param name="instanceName">The singleton property name to get the instance to use from</param>
510
[Conditional("EQUATABLE_GENERATOR")]
611
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
712
public class EqualityComparerAttribute(Type equalityType, string instanceName = "Default") : Attribute
813
{
14+
/// <summary>
15+
/// The <see cref="IEqualityComparer{T}"/> type to use
16+
/// </summary>
917
public Type EqualityType { get; } = equalityType;
1018

19+
/// <summary>
20+
/// The singleton property name to get the instance to use from
21+
/// </summary>
1122
public string InstanceName { get; } = instanceName;
1223
}

src/Equatable.Generator/Attributes/EquatableAttribute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Equatable.Attributes;
44

5+
/// <summary>
6+
/// Marks the class to source generate overrides for Equals and GetHashCode.
7+
/// </summary>
58
[Conditional("EQUATABLE_GENERATOR")]
69
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
710
public class EquatableAttribute : Attribute;
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
22

33
namespace Equatable.Attributes;
44

5+
/// <summary>
6+
/// Use <see cref="ISet{T}.SetEquals(IEnumerable{T})"/> in Equals and GetHashCode implementations
7+
/// </summary>
58
[Conditional("EQUATABLE_GENERATOR")]
69
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
710
public class HashSetEqualityAttribute : Attribute;
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
22

33
namespace Equatable.Attributes;
44

5+
/// <summary>
6+
/// Ignore property in Equals and GetHashCode implementations
7+
/// </summary>
58
[Conditional("EQUATABLE_GENERATOR")]
69
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
710
public class IgnoreEqualityAttribute : Attribute;
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
22

33
namespace Equatable.Attributes;
44

5+
/// <summary>
6+
/// Use <see cref="Object.ReferenceEquals(object, object)"/> to determines whether instances are the same instance
7+
/// </summary>
58
[Conditional("EQUATABLE_GENERATOR")]
69
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
710
public class ReferenceEqualityAttribute : Attribute;

src/Equatable.Generator/Attributes/SequenceEqualityAttribute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Equatable.Attributes;
44

5+
/// <summary>
6+
/// Use <see cref="Enumerable.SequenceEqual{TSource}(IEnumerable{TSource}, IEnumerable{TSource})"/> in Equals and GetHashCode implementations
7+
/// </summary>
58
[Conditional("EQUATABLE_GENERATOR")]
69
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
710
public class SequenceEqualityAttribute : Attribute;
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
22

33
namespace Equatable.Attributes;
44

5+
/// <summary>
6+
/// Use specified <see cref="StringComparer" /> when comparing strings
7+
/// </summary>
8+
/// <param name="comparisonType">The <see cref="StringComparison"/> to use</param>
59
[Conditional("EQUATABLE_GENERATOR")]
610
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
711
public class StringEqualityAttribute(StringComparison comparisonType) : Attribute
812
{
13+
/// <summary>
14+
/// The <see cref="StringComparison"/> to use
15+
/// </summary>
916
public StringComparison ComparisonType { get; } = comparisonType;
1017
}

0 commit comments

Comments
 (0)