AddWithXmlDoc.Core
2.1.0
dotnet add package AddWithXmlDoc.Core --version 2.1.0
NuGet\Install-Package AddWithXmlDoc.Core -Version 2.1.0
<PackageReference Include="AddWithXmlDoc.Core" Version="2.1.0" />
<PackageVersion Include="AddWithXmlDoc.Core" Version="2.1.0" />
<PackageReference Include="AddWithXmlDoc.Core" />
paket add AddWithXmlDoc.Core --version 2.1.0
#r "nuget: AddWithXmlDoc.Core, 2.1.0"
#:package AddWithXmlDoc.Core@2.1.0
#addin nuget:?package=AddWithXmlDoc.Core&version=2.1.0
#tool nuget:?package=AddWithXmlDoc.Core&version=2.1.0
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 | 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. 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. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 3.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Changed the following in generated code:
```cs
public class C : IEquatable<C>
```
to:
```cs
public class C : IEquatable<C?>
```