All Projects → diegofrata → Generator.Equals

diegofrata / Generator.Equals

Licence: MIT license
A source code generator for automatically implementing IEquatable<T> using only attributes.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to Generator.Equals

DpdtInject
Highly efficient compile-time general purpose DI container based on C# source generators.
Stars: ✭ 25 (-48.98%)
Mutual labels:  source-generators, csharp-sourcegenerator
AutoInterface
C# interface-to-member source generator
Stars: ✭ 47 (-4.08%)
Mutual labels:  source-generators, csharp-sourcegenerator
nunit.analyzers
Roslyn analyzers for writing unit tests with NUnit
Stars: ✭ 69 (+40.82%)
Mutual labels:  roslyn-analyzer
UdonRabbit.Analyzer
DEPRECATED (U#1.0 not supported) .NET Roslyn Analyzer for VRChat Udon and UdonSharp.
Stars: ✭ 44 (-10.2%)
Mutual labels:  roslyn-analyzer
JsonByExampleGenerator
Generate classes based on example json files in your project. Uses a C# 9 source generator.
Stars: ✭ 55 (+12.24%)
Mutual labels:  csharp-sourcegenerator
spreadcheetah
SpreadCheetah is a high-performance .NET library for generating spreadsheet (Microsoft Excel XLSX) files.
Stars: ✭ 107 (+118.37%)
Mutual labels:  csharp-sourcegenerator
dart sealed
Dart and Flutter sealed class generator and annotations, with match methods and other utilities. There is also super_enum compatible API.
Stars: ✭ 16 (-67.35%)
Mutual labels:  equality
Go Cmp
Package for comparing Go values in tests
Stars: ✭ 2,611 (+5228.57%)
Mutual labels:  equality
ZeroIoC
ZeroIoC is reflectionless IoC Container for .NET
Stars: ✭ 22 (-55.1%)
Mutual labels:  roslyn-analyzer
blazorators
This project converts TypeScript type declarations into C# representations, and use C# source generators to expose automatic JavaScript interop functionality.
Stars: ✭ 225 (+359.18%)
Mutual labels:  source-generators
JsonSrcGen
Json library that uses .NET 5 Source Generators
Stars: ✭ 140 (+185.71%)
Mutual labels:  csharp-sourcegenerator
Vogen
A semi-opinionated library which is a source generator and a code analyser. It Source generates Value Objects
Stars: ✭ 240 (+389.8%)
Mutual labels:  csharp-sourcegenerator
Analyzers
C# code analyzers
Stars: ✭ 18 (-63.27%)
Mutual labels:  roslyn-analyzer
dotvariant
A type-safe and space-efficient sum type for C# (comparable to discriminated unions in C or C++)
Stars: ✭ 52 (+6.12%)
Mutual labels:  csharp-sourcegenerator
WinFormsComInterop
ComWrappers required to run NativeAOT and WinForms
Stars: ✭ 54 (+10.2%)
Mutual labels:  csharp-sourcegenerator
ExhaustiveMatching
C# Analyzer Adding Exhaustive Checking of Switch Statements and Expressions
Stars: ✭ 60 (+22.45%)
Mutual labels:  roslyn-analyzer
ThisAssembly
Exposes project and assembly level information as constants in the ThisAssembly class using source generators powered by Roslyn.
Stars: ✭ 209 (+326.53%)
Mutual labels:  csharp-sourcegenerator
GodotOnReady
A C# Source Generator that adds convenient onready-like features to your C# scripts in Godot Mono without any reflection.
Stars: ✭ 73 (+48.98%)
Mutual labels:  csharp-sourcegenerator
EmbeddingResourceCSharp
Embed resource files more C# programmer friendly!
Stars: ✭ 22 (-55.1%)
Mutual labels:  csharp-sourcegenerator
Immutype
Immutability is easy!
Stars: ✭ 26 (-46.94%)
Mutual labels:  csharp-sourcegenerator

Nuget

Generator.Equals

A source code generator for automatically implementing IEquatable<T> using only attributes.


Requirements

In order to use this library, you must:

  • Use a target framework that supports .NET Standard >= 2.0 (eg. .NET Core 3.1 or .NET 5.0);
  • Set your project's C# LangVersion property to 8.0 or higher.

Usage

The below sample shows how to use Generator.Equals to override the default equality implementation for a C# record, enhancing it with the ability to determine the equality between the array contents of the record.

using Generator.Equals;

[Equatable]
partial record MyRecord(
    [property: OrderedEquality] string[] Fruits
);

class Program
{
    static void Main(string[] args)
    {
        var record1 = new MyRecord(new[] {"banana", "apple"});
        var record2 = new MyRecord(new[] {"banana", "apple"});

        Console.WriteLine(record1 == record2);
    }
}

Not using records? Generator.Equals also support classes.

using Generator.Equals;

[Equatable]
partial class MyClass
{
    [OrderedEquality] 
    public string[] Fruits { get; set; }
}

Supported Comparers

Below is a list of all supported comparers. Would you like something else added? Let me know by raising an issue or sending a PR!

Default

This is the comparer that's used when a property has no attributes indicating otherwise. The generated code will use EqualityComparer<T>.Default for both equals and hashing operation.

IgnoreEquality

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

As the name implies, the property is ignored during Equals and GetHashCode calls!

OrderedEquality

[OrderedEquality] 
public string[] Fruits { get; set; } // Fruits have to be in the same order for the array to be considered equal.

This equality comparer will compare properties as a sequence instead of a reference. This works just like Enumerable.SequenceEqual, which assumes both lists are of the same size and same sort.

Bear in mind that the property has to implement IEnumerable and the that the items themselves implement equality (you can use Generator.Equals in the items too!).

UnorderedEquality

[UnorderedEquality] 
public string[] Fruits { get; set; } // Does not care about the order of the fruits!

[UnorderedEquality] 
public IDictionary<string, object> Properties { get; set; } // Works with dictionaries too!

This equality comparer will compare properties as an unordered sequence instead of a reference. This works just like Enumerable.SequenceEqual, but it does not care about the order as long as the all values (including the repetitions) are present.

As with OrderedEquality, bear in mind that the property (or key and values if using a dictionary) has to implement IEnumerable and the that the items themselves implement equality (you can use Generator.Equals in the items too!).

SetEquality

[SetEquality] 
public HashSet<string> Fruits { get; set; } // Fruits can be in any order and it can be repeated

This equality comparer will do a set comparison, using SetEquals whenever the underlying collection implements ISet<T>, otherwise falling back to manually comparing both collections, which can be expensive for large collections.

Hashing always returns 0 for this type of equality,

ReferenceEquality

[ReferenceEquality] 
public string Name { get; set; } // Will only return true if strings are the same reference (eg. when used with string.Intern)

This will ignore whatever equality is implemented for a particular object and compare references instead.

CustomEquality

class LengthEqualityComparer : IEqualityComparer<string>
{
    public static readonly LengthEqualityComparer Default = new();

    public bool Equals(string? x, string? y) => x?.Length == y?.Length;

    public int GetHashCode(string obj) => obj.Length.GetHashCode();
}

class NameEqualityComparer 
{
    public static readonly IEqualityComparer<string> Default = new SomeCustomComparer();
}


[CustomEquality(typeof(LengthEqualityComparer))] 
public string Name1 { get; set; } // Will use LengthEqualityComparer to compare the values of Name1.

[CustomEquality(typeof(NameEqualityComparer))] 
public string Name2 { get; set; } // Will use NameEqualityComparer.Default to compare values of Name2.

[CustomEquality(typeof(StringComparer), nameof(StringComparer.OrdinalIgnoreCase))] 
public string Name2 { get; set; } // Will use StringComparer.OrdinalIgnoreCase to compare values of Name2.

This attribute allows you to specify a custom comparer for a particular property. For it to work, the type passed as an argument to CustomEqualityAttribute should fulfill AT LEAST one of the following:

  • Have a static field/property named Default returning a valid IEqualityComparer instance for the target type;
  • Have a static field/property with the same name passed to the CustomComparerAttribute returning a valid IEqualityComparer instance for the target type;
  • Implement IEqualityComparer and expose a parameterless constructor.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].