|
| 1 | +# 📃 AddWithXmlDoc |
| 2 | +A Visual Studio extension that lets you generate common methods in C# - parameterless constructors, `GetHashCode`, `Equals`, and equality operators - with XML documentation automatically inserted for you. |
| 3 | + |
| 4 | +# ❓ How does it work? |
| 5 | +When you install the extension, you get four new code actions for classes and structs! |
| 6 | + |
| 7 | +- `XML: Add Equality Methods to Fields`: When you click this, it generates `GetHashCode`, `Equals`, `IEquatable`, and equality operators for all fields, + XML documentation. |
| 8 | +- `XML: Add Equality Methods to Properties`: Same as the one above, but for properties. |
| 9 | +- `XML: Add Equality Methods to Members`: Same as the one above, but for both Fields and Properties. |
| 10 | +- `XML: Add Parameterless Constructor`: Adds an empty constructor without any parameters, with XML documentation |
| 11 | + |
| 12 | +You no longer have to manually write XML documentation for methods like `GetHashCode` or `Equals` - even an `<inheritdoc ... />` can take some time to write! AddWithXmlDoc does that for you. |
| 13 | + |
| 14 | +# ✨ Example |
| 15 | +Imagine we have a class: |
| 16 | +```cs |
| 17 | +public class Class1 |
| 18 | +{ |
| 19 | + public string Greeting { get; set; } |
| 20 | + = "Welcome to AddWithXmlDoc!" |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +Right click on `Class1`. Click `XML: Add Equality Methods to Members`. |
| 25 | +Our class now looks like this. With just one click. |
| 26 | + |
| 27 | +```cs |
| 28 | +public class Class1 : IEquatable<Class1> |
| 29 | +{ |
| 30 | + public string Greeting { get; set; } |
| 31 | + = "Welcome to AddWithXmlDoc!" |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Determines if this instance is equal to the other instance of type <see cref = "Class1"/>. |
| 35 | + /// </summary> |
| 36 | + /// <param name = "other">The other value to compare this instance with.</param> |
| 37 | + /// <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> |
| 38 | + public override bool Equals(object? other) |
| 39 | + { |
| 40 | + return other is Class1 value && Equals(value); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Determines if this instance is equal to the other instance of type <see cref = "Class1"/>. |
| 45 | + /// </summary> |
| 46 | + /// <param name = "other">The other instance to compare this instance with.</param> |
| 47 | + /// <returns>A boolean that, if <see langword="true"/>, indicates that <paramref name = "other"/> is equal to this instance.</returns> |
| 48 | + public bool Equals(Class1? other) |
| 49 | + { |
| 50 | + return this.Greeting == other?.Greeting; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Computes the hash code for this object. |
| 55 | + /// </summary> |
| 56 | + /// <returns>This object's hash code.</returns> |
| 57 | + public override int GetHashCode() |
| 58 | + { |
| 59 | + var hashCode = new HashCode(); |
| 60 | + hashCode.Add(this.Greeting); |
| 61 | + return hashCode.ToHashCode(); |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Determines if <paramref name = "left"/> is equal to <paramref name = "right"/>. |
| 66 | + /// </summary> |
| 67 | + /// <param name = "left">The value to compare from.</param> |
| 68 | + /// <param name = "right">The value to compare with.</param> |
| 69 | + /// <returns><see langword="true"/> if the left parameter is same as the right parameter, otherwise <see langword="false"/>.</returns> |
| 70 | + public static bool operator ==(Class1 left, Class1 right) |
| 71 | + { |
| 72 | + return left.Equals(right); |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// Determines if <paramref name = "left"/> is not equal to <paramref name = "right"/>. |
| 77 | + /// </summary> |
| 78 | + /// <param name = "left">The value to compare from.</param> |
| 79 | + /// <param name = "right">The value to compare with.</param> |
| 80 | + /// <returns><see langword="true"/> if the left parameter is different compared to the right parameter, otherwise <see langword="false"/> if both are same.</returns> |
| 81 | + public static bool operator !=(Class1 left, Class1 right) |
| 82 | + { |
| 83 | + return !(left == right); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Typing all of this would've took a while! |
| 89 | + |
| 90 | +Or, instead, click `XML: Add Parameterless Constructor`. Our class now looks like this. |
| 91 | + |
| 92 | +```cs |
| 93 | +public class Class1 |
| 94 | +{ |
| 95 | + public string Greeting { get; set; } |
| 96 | + = "Welcome to AddWithXmlDoc!" |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Initializes a new instance of the <see cref="Class1" /> class. |
| 100 | + /// </summary> |
| 101 | + public Class1() |
| 102 | + { |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +# 🧱 Prerequisites |
| 108 | +AddWithXmlDoc supports all Visual Studio versions starting from Visual Studio 2017. You should have |
| 109 | +Visual Studio versions at least 2017 to use AddWithXmlDoc. |
| 110 | + |
| 111 | +# 📦 Building |
| 112 | +You'll have to have Visual Studio workloads `.NET Desktop Development` and `Visual Studio extension development` prior to building AddWithXmlDoc. |
| 113 | +After that, open the solution VSAddWithXmlDoc.sln, and click Ctrl+Shift+B in Visual Studio to build the entire solution! |
| 114 | + |
| 115 | +# ❔ Got any questions? |
| 116 | +Feel free to create an Issue post on the GitHub Repository if you have any questions, found a bug, or proposing a suggestion! |
| 117 | + |
| 118 | +# 🐞 Known Issues |
| 119 | +One known problem is that when you click to generate equality methods, the class's base list can become incomplete, like so: |
| 120 | +```cs |
| 121 | +public class Class1 : |
| 122 | +``` |
| 123 | +This seems to be a bug with the `NormalizeWhitespace()` method in Roslyn. The issue is that we're using an older version, 3.3.1, because |
| 124 | +apparently Visual Studio does not load AddWithXmlDoc if it uses a newer version of Roslyn (i.e. 4.14.0), so we have to use 3.3.1. If you're aware |
| 125 | +on potential fixes of this problem, please let me know by creating an Issue post on the GitHub Repository! |
| 126 | + |
| 127 | +But it's not a major problem. It just takes 2-4 seconds to manually add: |
| 128 | +```cs |
| 129 | +public class Class1 : IEquatable<Class1> |
| 130 | +``` |
| 131 | + |
| 132 | +# 🤗 Author |
| 133 | +[winscripter](https://github.com/winscripter) |
| 134 | + |
| 135 | +# 🪪 License |
| 136 | +Copyright (c) 2023-2025, winscripter |
| 137 | + |
| 138 | +See the file LICENSE.txt for licensing information. |
0 commit comments