对委托声明事件的 XML 注释

发布于 2024-08-25 16:06:19 字数 1388 浏览 9 评论 0原文

我正在访问一些旧代码,并且有相当多的事件是用委托手动声明的,而不是使用 EventHandler,如下所示:

/// <summary>
/// Delegate for event Added
/// </summary>
/// <param name="index">Index of the item</param>
/// <param name="item">The item itself</param>
public delegate void ItemAdded(int index, T item);

/// <summary>
/// Added is raised whenever an item is added to the collection
/// </summary>
public event ItemAdded Added;

一切都很好,直到我开始使用 sandcastle 来记录库,因为它可以'找不到由事件声明生成的私有“已添加”字段的任何 XML 注释。我想尝试解决这个问题,但我想做的是:

  • 让 sandcastle 忽略自动生成的私有字段,而不告诉它完全忽略所有私有字段,

或者

  • 获取为私有字段生成的 XML 注释

是否有无需重构代码即可实现此目的的方法如下:

/// <summary>
/// Delegate for event <see cref="Added"/>
/// </summary>
/// <param name="index">Index of the item</param>
/// <param name="item">The item itself</param>
public delegate void ItemAdded(int index, T item);

/// <summary>
/// Private storage for the event firing delegate for the <see cref="Added"/> event
/// </summary>
private ItemAdded _added;

/// <summary>
/// Added is raised whenever an item is added to the collection
/// </summary>
public event ItemAdded Added
{
    add
    {
        _added += value;
    }
    remove
    {
        _added -= value;
    }
}

I am visiting some old code, and there are quite a few events declared with delegates manually rather than using EventHandler<T>, like this:

/// <summary>
/// Delegate for event Added
/// </summary>
/// <param name="index">Index of the item</param>
/// <param name="item">The item itself</param>
public delegate void ItemAdded(int index, T item);

/// <summary>
/// Added is raised whenever an item is added to the collection
/// </summary>
public event ItemAdded Added;

All well and good, until I come to use sandcastle to document the library, because it then can't find any XML comments for the private Added field that is generated by the event declaration. I want to try and sort that out, but what I would like to do is either:

  • Get sandcastle to ignore the auto-generated private field without telling it to ignore all private fields entirely

or

  • Get XML comments generated for the private field

Is there any way of achieving this without re-factoring the code to look like this:

/// <summary>
/// Delegate for event <see cref="Added"/>
/// </summary>
/// <param name="index">Index of the item</param>
/// <param name="item">The item itself</param>
public delegate void ItemAdded(int index, T item);

/// <summary>
/// Private storage for the event firing delegate for the <see cref="Added"/> event
/// </summary>
private ItemAdded _added;

/// <summary>
/// Added is raised whenever an item is added to the collection
/// </summary>
public event ItemAdded Added
{
    add
    {
        _added += value;
    }
    remove
    {
        _added -= value;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗫嚅 2024-09-01 16:06:19

虽然不是最理想的,但我找到了解决这个问题的方法,即手动将 XML 注释放入包含项目命名空间级文档的文件中,大致如下:

<member name="F:myCompany.Common.Collections.Generic.EventableCollection`1.Added">
  <summary>
    Auto-generated backing field for the <see cref="E:myCompany.Common.Collections.Generic.EventableSortedList`1.Added">Added</see> event
  </summary>
</member>

这样就大致给出了我所需要的内容。

Although sub-optimal, I found a way around this, which was to manually put the XML comments into the file that contains namespace-level documentation for the project, along these lines:

<member name="F:myCompany.Common.Collections.Generic.EventableCollection`1.Added">
  <summary>
    Auto-generated backing field for the <see cref="E:myCompany.Common.Collections.Generic.EventableSortedList`1.Added">Added</see> event
  </summary>
</member>

This then gives roughly what I needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文