AddWithXmlDoc.Core 2.1.0

dotnet add package AddWithXmlDoc.Core --version 2.1.0
                    
NuGet\Install-Package AddWithXmlDoc.Core -Version 2.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AddWithXmlDoc.Core" Version="2.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AddWithXmlDoc.Core" Version="2.1.0" />
                    
Directory.Packages.props
<PackageReference Include="AddWithXmlDoc.Core" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AddWithXmlDoc.Core --version 2.1.0
                    
#r "nuget: AddWithXmlDoc.Core, 2.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AddWithXmlDoc.Core@2.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AddWithXmlDoc.Core&version=2.1.0
                    
Install as a Cake Addin
#tool nuget:?package=AddWithXmlDoc.Core&version=2.1.0
                    
Install as a Cake Tool

AddWithXmlDoc.Core

This is the core library for AddWithXmlDoc, a Visual Studio extension to add XML-documented members to classes.

It currently supports C#.

Usage

Start by parsing an input class.

Source (as a string variable named inputCode):

public class MyClass
{
    public string MyString { get; set; } = "Hello, World!";
    public int MyNumber { get; set; } = 42;
    public bool MyBoolean { get; set; } = true;
    public double MyDouble { get; set; } = 3.14;
    public DateTime MyDateTime { get; set; } = DateTime.Now;
}

Parse it and get the TypeDeclarationSyntax:

using Microsoft.CodeAnalysis.CSharp;

var declaration = CSharpSyntaxTree.ParseText(inputCode)
    .GetRoot()
    .DescendantNodes()
    .OfType<TypeDeclarationSyntax>()
    .First();

Create a LanguageServicesContainer instance for C#.

var container = LanguageServicesContainer.CreateCSharp();

Use the AddEqualityMembersToMembers method to add equality members to fields and properties.

container.AddEqualityMembersToMembers.ProvideRootNode(declaration);
container.AddEqualityMembersToMembers.UseInheritdocWherePossible = false; // set to true if you want to use inheritdoc
container.AddEqualityMembersToMembers.Invoke(
    (result) =>
    {
        Console.WriteLine(result.ToString());
    });

Result:

public class MyClass : IEquatable<MyClass>
{
    public string MyString { get; set; } = "Hello, World!";
    public int MyNumber { get; set; } = 42;
    public bool MyBoolean { get; set; } = true;
    public double MyDouble { get; set; } = 3.14;
    public DateTime MyDateTime { get; set; } = DateTime.Now;

    /// <summary>
    /// Determines if this instance is equal to the other instance of type <see cref = "MyClass"/>.
    /// </summary>
    /// <param name = "other">The other value to compare this instance with.</param>
    /// <returns>A boolean that, if <see langword="true"/>, indicates that <paramref name = "other"/> is equal to this instance, is of same type as this instance, and is not null.</returns>
    public override bool Equals(object? other)
    {
        return other is MyClass value && Equals(value);
    }

    /// <summary>
    /// Determines if this instance is equal to the other instance of type <see cref = "MyClass"/>.
    /// </summary>
    /// <param name = "other">The other instance to compare this instance with.</param>
    /// <returns>A boolean that, if <see langword="true"/>, indicates that <paramref name = "other"/> is equal to this instance.</returns>
    public bool Equals(MyClass? other)
    {
        return this.MyString == other?.MyString && this.MyNumber == other?.MyNumber && this.MyBoolean == other?.MyBoolean && this.MyDouble == other?.MyDouble && this.MyDateTime == other?.MyDateTime;
    }

    /// <summary>
    /// Computes the hash code for this object.
    /// </summary>
    /// <returns>This object's hash code.</returns>
    public override int GetHashCode()
    {
        var hashCode = new HashCode();
        hashCode.Add(this.MyString);
        hashCode.Add(this.MyNumber);
        hashCode.Add(this.MyBoolean);
        hashCode.Add(this.MyDouble);
        hashCode.Add(this.MyDateTime);
        return hashCode.ToHashCode();
    }

    /// <summary>
    /// Determines if <paramref name = "left"/> is equal to <paramref name = "right"/>.
    /// </summary>
    /// <param name = "left">The value to compare from.</param>
    /// <param name = "right">The value to compare with.</param>
    /// <returns><see langword="true"/> if the left parameter is same as the right parameter, otherwise <see langword="false"/>.</returns>
    public static bool operator ==(MyClass left, MyClass right)
    {
        return left.Equals(right);
    }

    /// <summary>
    /// Determines if <paramref name = "left"/> is not equal to <paramref name = "right"/>.
    /// </summary>
    /// <param name = "left">The value to compare from.</param>
    /// <param name = "right">The value to compare with.</param>
    /// <returns><see langword="true"/> if the left parameter is different compared to the right parameter, otherwise <see langword="false"/> if both are same.</returns>
    public static bool operator !=(MyClass left, MyClass right)
    {
        return !(left == right);
    }
}

Use AddEqualityMembersToFields to only target fields, and AddEqualityMembersToProperties to only target properties.

Use AddParameterlessConstructor to add a parameterless constructor. Usage is the same as for AddEqualityMembersToMembers. Result:

public class MyClass
{
    public string MyString { get; set; } = "Hello, World!";
    public int MyNumber { get; set; } = 42;
    public bool MyBoolean { get; set; } = true;
    public double MyDouble { get; set; } = 3.14;
    public DateTime MyDateTime { get; set; } = DateTime.Now;

    /// <summary>
    /// Initializes a new instance of the <see cref="MyClass" /> class.
    /// </summary>
    public MyClass()
    {
    }
}
Product 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.1.0 168 8/28/2025
2.0.0 164 8/28/2025
1.0.1 163 8/28/2025

Changed the following in generated code:
```cs
public class C : IEquatable<C>
```
to:
```cs
public class C : IEquatable<C?>
```