In C#, is there a way to centralize the definition of XML parameter descriptors for use across multiple methods?

35 Views Asked by At

I understand how to use XML tags in order to populate Intellisense descriptions when calling methods. I would like to centralize the definitions of various parameters (6 of them) however so that I don't need to copy/paste the same XML tags across dozens of formulas with similar parameters.

It would also be helpful to define them as a single list, so that each method only has a single decorator, such as [MyCommonParameters], instead of 6 lines of definitions on each method.

For example, in this simplified code below, I have two functions which take the same inputs. It would be nice to only have to define the parameter descriptions once, for better maintainability once brought up to scale for the full project.

public static class MyClass
{
    /// <param name="x">x description</param>
    /// <param name="y">y description</param>
    public static int Add_XY(int x, int y)
    {
        return x + y;
    }

    /// <param name="x">x description</param>
    /// <param name="y">y description</param>
    public static int Subtract_XY(int x, int y)
    {
        return x - y;
    }
}

I have tried using decorators/attributes, but got stuck as the inputs seem to require static text, and can't be passed a variable containing the definition. I don't have a strong grasp on using attributes yet, so I may just be missing how it could be implemented using custom attributes.

1

There are 1 best solutions below

1
Matthew Watson On BEST ANSWER

Yes, there is a way, as described here.

For example, you could add a file to your project called "CodeDoc.xml" with the following contents:

<?xml version="1.0" encoding="utf-8" ?>
<doc>
  <members>
    <member name="M:MyXYParams">
      <param name="x">Description of 'x' parameter</param>
      <param name="y">Description of 'y' parameter</param>
    </member>
  </members>
</doc>

And then change your XML comments to reference it like so:

public static class MyClass
{
    /// <include file="CodeDoc.xml" path="doc/members/member[@name='M:MyXYParams']" />
    public static int Add_XY(int x, int y)
    {
        return x + y;
    }

    /// <include file="CodeDoc.xml" path="doc/members/member[@name='M:MyXYParams']" />
    public static int Subtract_XY(int x, int y)
    {
        return x - y;
    }
}

You can change the M:MyXYParams to suit, but it must begin with M: as far as I know.