简体   繁体   English

C# XML 注释重用

[英]C# XML Comment Reuse

I'm writing a C# class library with a number of classes with functions that do just about the same stuff.我正在编写一个 C# 类库,其中包含许多具有相同功能的类。 I need to provide XML comments about the function arguments in each class which are very detailed but the same in most cases.我需要为每个类中的函数参数提供 XML 注释,这些注释非常详细,但在大多数情况下是相同的。 Is there a way of reusing XML comments so I don't have to repeat these XML argument definitions all over my assembly?有没有办法重用 XML 注释,这样我就不必在整个程序集中重复这些 XML 参数定义?

Here's an example of my classes:这是我的课程的一个例子:

public class IsodoseControl : TestModule
{
    /// <summary>
    /// Verify a control on Isodose dialog
    /// </summary>
    /// <param name="args">  **<-- WHAT I DON'T WANT TO KEEP REPEATING**
    /// Arguments: [Property, Condition, Expected Value, Tolerance]
    ///            Properties: STATE, VALUE, LABEL
    ///            Conditions: Exists, DoesNotExist, IsEnabled, IsDisabled, ...
    ///            Expected Value (optional): blah blah
    ///            Tolerance (optional): blah blah blah
    /// </param>
    public VerifResult VerifyIsodoseControl(string[] args)
    {
        ...
    }
}

public class BeamControl : TestModule
{
    /// <summary>
    /// Verify a control on Beam dialog
    /// </summary>
    /// <param name="args">  **<-- WHAT I DON'T WANT TO KEEP REPEATING**
    /// Arguments: [Property, Condition, Expected Value, Tolerance]
    ///            Properties: STATE, VALUE, LABEL
    ///            Conditions: Exists, DoesNotExist, IsEnabled, IsDisabled, ...
    ///            Expected Value (optional): blah blah
    ///            Tolerance (optional): blah blah blah
    /// </param>
    public VerifResult VerifyBeamControl(string[] args)
    {
        ...
    }
}

Thanks谢谢

I don't think there's anything in Visual Studio that will help you. 我认为Visual Studio中没有任何东西可以帮助您。 Sandcastle has a tag, inheritdoc , that will let you inherit entire blocks of xml comments, or you might also define a sandcastle token that contains your param text, which would let you write something like 沙堡有一个标签, inheritdoc ,这将让你继承的XML注释整块,或者你可能还定义了一个沙堡令牌包含您的PARAM文本,这将让你喜欢写东西

/// <summary>
/// Verify a control on Beam dialog
/// </summary>
/// <param name="args"><token>CommonParamInfo</token></param>
/// (...)

Sandcastle was designed specifically for API documentation, and might not be appropriate for your case, though. Sandcastle专门针对API文档而设计,但可能不适合您的情况。

"The <include> tag lets you refer to comments in another file that describe the types and members in your source code. " “<include>标签允许您引用另一个描述源代码中类型和成员的文件中的注释。”

You could reference the same file from both classes using <include> tags. 您可以使用<include>标记从两个类中引用相同的文件。

/// <include file='comments.xml' path='MyDocs/MyMembers[@name="test"]/*' />
class OneClass {}

/// <include file='comments.xml' path='MyDocs/MyMembers[@name="test"]/*' />
class DifferentClassWithTheSameFunctionality {}

This link provides some examples of using <include>: http://msdn.microsoft.com/en-us/library/9h8dy30z.aspx 此链接提供了一些使用<include>的示例: http//msdn.microsoft.com/en-us/library/9h8dy30z.aspx

You can use <inheritdoc .../> to get pretty close.您可以使用<inheritdoc .../>来非常接近。 It seems mainly limited to only reproducing the entire documentation entry, but with that + some careful wording perhaps it can be made to work.它似乎主要仅限于仅复制整个文档条目,但有了这个 + 一些谨慎的措辞,也许它可以工作。

(Slightly modified wording certainly could be much better than documentation gradually falling out of date because of overlooked manual upkeep.) (稍微修改的措辞肯定比由于忽视手动维护而逐渐过时的文档要好得多。)

<inheritdoc>

XML XML

<inheritdoc [cref=""] [path=""]/>

Inherit XML comments from base classes, interfaces, and similar methods.从基类、接口和类似方法继承 XML 注释。 Using inheritdoc eliminates unwanted copying and pasting of duplicate XML comments and automatically keeps XML comments synchronized.使用inheritdoc 消除了不必要的复制和粘贴重复的XML 注释,并自动保持XML 注释同步。

... ...

cref : Specify the member to inherit documentation from. cref :指定要从中继承文档的成员。 Already defined tags on the current member are not overridden by the inherited ones.当前成员上已经定义的标签不会被继承的标签覆盖。

path : The XPath expression query that will result in a node set to show. path :XPath 表达式查询将导致节点集显示。 You can use this attribute to filter the tags to include or exclude from the inherited documentation.您可以使用此属性过滤要从继承的文档中包含或排除的标签。

... ...

Reference: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/xmldoc/recommended-tags#inheritdoc参考: https ://docs.microsoft.com/en-us/dotnet/csharp/language-reference/xmldoc/recommended-tags#inheritdoc

When you start entering the value for cref your will get intellisense.当您开始输入cref的值时,您将获得智能感知。 It is entirely possible (from a quick test I just did) to specify a namespace.classname.methodname there so that it can refer to an entirely different class.完全有可能(从我刚刚做的快速测试)在那里指定一个namespace.classname.methodname ,以便它可以引用一个完全不同的类。

So in the example from the question you could do:因此,在问题的示例中,您可以执行以下操作:

    public class IsodoseControl : TestModule
    {
        /// <summary>
        /// Verify a control on the dialog
        /// </summary>
        /// <param name="args">
        /// Arguments: [Property, Condition, Expected Value, Tolerance]
        ///            Properties: STATE, VALUE, LABEL
        ///            Conditions: Exists, DoesNotExist, IsEnabled, IsDisabled, ...
        ///            Expected Value (optional): blah blah
        ///            Tolerance (optional): blah blah blah
        /// </param>
        public VerifResult VerifyIsodoseControl(string[] args)
        {
            ///...
        }
    }

    public class BeamControl : TestModule
    {
        /// <inheritdoc cref="IsodoseControl.VerifyIsodoseControl" />
        public VerifResult VerifyBeamControl(string[] args)
        {
            // ...
        }
    }

For this to make sense, I just removed the word "Isodose" from the "Verify a control on..." sentence.为此,我刚刚从“验证...上的控件”句子中删除了“等剂量”一词。 You'll have to decide in your own code if such a rewording is sufficiently clear.如果这样的改写足够清晰,您必须在自己的代码中做出决定。

Here's how the intellisense shows up for VerifyBeamControl() showing the docs:以下是显示文档的VerifyBeamControl()智能感知的显示方式: 在此处输入图像描述


I did not experiment with the path attribute.我没有尝试使用path属性。 Examples I saw made it seem pretty unreadable, and for my own use that might be a cure worse than the problem.我看到的例子让它看起来很不可读,而且对于我自己的使用来说,这可能是一种比问题更糟糕的治疗方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM