Zodt / EquatableSourceGenerator

The `EquatableSourceGenerator` is a simple generator that realize the implementation of the interface IEquatable`1 in a partial class

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Warning: This project is proof of concept.

License: We are using the MIT License

What's EquatableSourceGenerator?

The `EquatableSourceGenerator` is a simple generator that implements the implementation of the interface IEquatable`1 in a partial class

Explanation of how it works:

To engage a class generator, class must be marked as partial and implement the IEquatable interface
public partial class DummyModel : IEqutable<DummyModel>
{
    //Your properties
}
The `EquatableSourceGenerator` will generate a partial class of `DummyModel`. It will like that:
using System;

namespace EquatableSourceGenerator.Sample.Models
{
    partial class DummyModel
    {
        public bool Equals(DummyModel? other)
        {
            return other is not null 
                /*&& All your properties*/;
        }
        public override bool Equals(object? obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            
            return obj.GetType() == this.GetType() 
                || obj is DummyModel self && Equals(self);
        }
        public override int GetHashCode()
        {
            HashCode hashCode = new();
	    /*&& hashCode will add all your properties*/;
	    return hashCode.ToHashCode();
        }

        public static bool operator == (DummyModel? self, DummyModel? other)
        {
            return other?.Equals(self) ?? self is null;
        }
        public static bool operator != (DummyModel? self, DummyModel? other)
        {
            return !(self == other);
        }
    }
}

About

The `EquatableSourceGenerator` is a simple generator that realize the implementation of the interface IEquatable`1 in a partial class

License:MIT License


Languages

Language:C# 100.0%