简体   繁体   English

参考 XML 文档注释中的通用类型

[英]Reference the Generic Type in an XML Documentation comment

I have a interface like this:我有一个这样的界面:

public IApiController<T> where T: IEntity
{
    /// <summary>
    /// Returns Entity with Id = <paramref name="id"/> of Class <typeparamref name="T"/>.
    /// </summary>
    public Task<ActionResult<T>> Get(int id);
}

In my comment I want to reference T , so when I have a DeveloperController that implements IApiController<Developer> the Intellisense comment says:在我的评论中我想引用T ,所以当我有一个实现IApiController<Developer> DeveloperController DeveloperController 时,Intellisense 评论说:

"Returns Entity with Id = id of Type Developer" “返回 Id = 类型开发人员 id 的实体”

but I only get the following comment:但我只得到以下评论:

"Returns Entity with Id = id of Type T" “返回 Id = id 类型 T 的实体”

Can anybody explain me what I am doing wrong?谁能解释我做错了什么?

Can anybody explain me what I am doing wrong?谁能解释我做错了什么?

It's hard to say, as your question lacks some important details.很难说,因为你的问题缺少一些重要的细节。 But, I suspect the code you didn't include in your post looks something like this:但是,我怀疑您未在帖子中包含的代码看起来像这样:

/// <inheritdoc/>
class DeveloperController : IApiController<Developer>
{
    public Task<ActionResult<Developer>> Get(int id) { /* ... */ }
}

and that when you view the Intellisense, you are viewing with a variable where the type is DeveloperController , and not IApiController<Developer> .并且当您查看 Intellisense 时,您正在查看类型为DeveloperController而不是IApiController<Developer>的变量。

The problem is that when the <inheritdoc/> markup inherits the XML comments from the original generic class, the <typeparameterref/> element necessarily still refers to the containing type's type parameter, and there is none.问题是当<inheritdoc/>标记从原始通用 class 继承 XML 注释时, <typeparameterref/>元素必然仍然引用包含类型的类型参数,并且没有。 It only has the XML comment itself at that point, not the type parameter that was used to construct the generic class.它只有 XML 注释本身,而不是用于构造通用 class 的类型参数。

So, for example, in the following code:因此,例如,在以下代码中:

IApiController<Developer> o1 = ...;
DeveloperController o2 = o1;

o1.Get(1);
o2.Get(1);

Intellisense will be able to tell you the actual type parameter when you hover over o1.Get(1) , but not when you hover over o2.Get(1) .当您 hover over o1.Get(1) 时,Intellisense 将能够告诉您实际的类型参数,但当您 hover over o1.Get(1) o2.Get(1)不能。

Note that if the DeveloperController type were itself generic and its type parameter were used for the declaration of the interface inheritance and of course the implementation, then Intellisense would have direct access to the generic type parameter and it would show what you expect.请注意,如果DeveloperController类型本身是泛型的,并且其类型参数用于接口 inheritance 的声明,当然还有实现,那么 Intellisense 将可以直接访问泛型类型参数,它会显示您所期望的。 Of course, making that type generic is probably not compatible with your design, so that doesn't really help in this case.当然,使该类型泛型可能与您的设计不兼容,因此在这种情况下并没有真正的帮助。 But it's worth keeping in mind for other scenarios.但对于其他情况,值得牢记。

It's also worth keeping in mind this limitation of <typeparameterref/> , because if you had yet another scenario, where the DeveloperController class was generic, but its type parameter was not used for the interface, but instead for some other generic aspect of the class, and calling code used a different type for that type parameter from the one used for the interface, Intellisense would display the wrong type .还值得记住<typeparameterref/>的这个限制,因为如果您还有另一种情况, DeveloperController class 是通用的,但它的类型参数用于接口,而是用于 class 的其他一些通用方面,并且调用代码对该类型参数使用了与用于接口的类型不同的类型, Intellisense 将显示错误的类型

For example, you had:例如,您有:

/// <inheritdoc/>
class DeveloperController<T> : IApiController<Developer>
{
    public Task<ActionResult<Developer>> Get(int id) { /* ... */ }
}

and then you used the DeveloperController<T> class with a type parameter of bool , Intellisense would display "Returns Entity with Id = id of Class bool."然后您使用DeveloperController<T> class 和bool类型参数,Intellisense 将显示“Returns Entity with Id = id of Class bool。”

Obviously, this is not ideal and in fact I would consider it a bug, however understandable it might be.显然,这并不理想,事实上我会认为它是一个错误,尽管它可能是可以理解的。

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

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