我得到这个警告:“缺少公开可见类型或成员的XML注释”。
如何解决这个问题?
我得到这个警告:“缺少公开可见类型或成员的XML注释”。
如何解决这个问题?
插入一个XML注释。: -)
/// <summary>
/// Describe your member here.
/// </summary>
public string Something
{
get;
set;
}
乍一看,这似乎是个笑话,但实际上可能很有用。对我来说,思考方法对于私有方法的作用是有帮助的(当然,除非非常琐碎)。
5个选项:
Fill in the documentation comments (great, but time-consuming) Turn off the comment generation (in project properties) Disable the warning in project properties (in 'Project properties' go to Project properties -> Build > "Errors and warnings" (section), Suppress Warnings (textbox), add 1591 (comma separated list)). By default it will change Active Configuration, consider to change configuration to All. Use #pragma warning disable 1591 to disable the warning just for some bits of code (and #pragma warning restore 1591 afterwards) Ignore the warnings (bad idea - you'll miss new "real" warnings)
当然要向公开可见的类型和成员添加XML注释:)
///<Summary>
/// Gets the answer
///</Summary>
public int MyMethod()
{
return 42;
}
所有成员都需要这些<summary>类型的注释——这些注释也会显示在智能感知弹出菜单中。
得到此警告的原因是因为您已经将项目设置为输出文档xml文件(在项目设置中)。这对于类库(.dll程序集)很有用,这意味着.dll的用户可以在visual studio中获得API的智能感知文档。
我建议你买一份GhostDoc Visual Studio插件。使记录变得更容易。
禁止XML注释的警告
(不是我的作品,但我发现它很有用,所以我包含了文章和链接)
http://bernhardelbl.wordpress.com/2009/02/23/suppress-warnings-for-xml-comments/
Here i will show you, how you can suppress warnings for XML comments after a Visual Studio build. Background If you have checked the "XML documentation file" mark in the Visual Studio project settings, a XML file containing all XML comments is created. Additionally you will get a lot of warnings also in designer generated files, because of the missing or wrong XML comments. While sometimes warnings helps us to improve and stabilize our code, getting hundreds of XML comment warnings is just a pain. Warnings Missing XML comment for publicly visible type or member … XML comment on … has a param tag for ‘…’, but there is no parameter by that name Parameter ‘…’ has no matching param tag in the XML comment for ‘…’ (but other parameters do) Solution You can suppress every warning in Visual Studio. Right-click the Visual Studio project / Properties / Build Tab Insert the following warning numbers in the "Suppress warnings": 1591,1572,1571,1573,1587,1570
我想对这里列出的答案补充一些东西:
正如Isak所指出的,XML文档对于类库非常有用,因为它为Visual Studio中的任何消费者提供了智能感知。因此,一个简单而正确的解决方案是关闭任何顶级项目(如UI等)的文档,因为它不会在自己的项目之外实现。
此外,我想指出的是,警告只表示在公开可见的成员。因此,如果将类库设置为只公开它需要公开的内容,则无需记录私有成员和内部成员。
在将一个属性附加到一个方法后,我得到了这条消息
[webMethod]
public void DoSomething()
{
}
但正确的做法是:
[webMethod()] // Note the Parentheses
public void DoSomething()
{
}
文件>编辑>查看项目(点击)
下拉弓的底部(点击打开/当前工作>属性), 打开项目属性页在“构建”下的“输出”。取消选中“XML文档”复选框。
重建,没有警告。
In your solution, once you check the option to generate XML Document file, it start checking your public members, for having the XMLDoc, if they don't, you'll receive a warning per each element. if you don't really want to release your DLL, and also you don't need documentations then, go to your solution, build section, and turn it off, else if you need it, so fill them, and if there are unimportant properties and fields, just surpass them with pre-compiler instruction #pragma warning disable 1591 you can also restore the warning : #pragma warning restore 1591
Pragma用法:在代码中任何你得到编译器警告的地方之前…(对于文件,把它放在头文件中,你不需要再次启用它,对于单个类环绕一个类,或方法环绕一个方法,或…你也不需要把它包装起来,你可以随意地调用它并恢复它(在文件的开始处开始,并在方法中结束)),编写以下代码:
#pragma警告禁用1591 如果你需要恢复它,使用: #pragma警告恢复1591
这里有一个例子:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using RealEstate.Entity.Models.Base;
namespace RealEstate.Models.Base
{
public class CityVM
{
#pragma warning disable 1591
[Required]
public string Id { get; set; }
[Required]
public string Name { get; set; }
public List<LanguageBasedName> LanguageBasedNames { get; set; }
[Required]
public string CountryId { get; set; }
#pragma warning restore 1591
/// <summary>
/// Some countries do not have neither a State, nor a Province
/// </summary>
public string StateOrProvinceId { get; set; }
}
}
注意,pragma指令从行首开始
I know this is a really old thread, but it's the first response on google so I thought I'd add this bit of information: This behavior only occurs when the warning level is set to 4 under "Project Properties" -> "Build". Unless you really need that much information you can set it to 3 and you'll get rid of these warnings. Of course, changing the warning level affects more than just comments, so please refer to the documentation if you're unsure what you'll be missing: https://msdn.microsoft.com/en-us/library/thxezb7y.aspx
#pragma warning disable 1591
#pragma warning disable 1591
#pragma warning disable 1572
#pragma warning disable 1571
#pragma warning disable 1573
#pragma warning disable 1587
#pragma warning disable 1570
这是因为您的项目属性中指定了一个XML文档文件,而您的方法/类是公共的,缺乏文档。 你可以:
禁用XML文档: 右键单击您的项目->属性->“构建”选项卡->取消选中XML文档文件。 坐下来自己写文档吧!
XML文档的总结如下:
/// <summary>
/// Description of the class/method/variable
/// </summary>
..declaration goes here..
您需要为显示警告的成员添加/// Comment。
参见下面的代码
public EventLogger()
{
LogFile = string.Format("{0}{1}", LogFilePath, FileName);
}
显示警告 缺少公开可见类型或成员'.EventLogger()'的XML注释
我为成员添加了评论,警告消失了。
///<Summary>
/// To write a log <Anycomment as per your code>
///</Summary>
public EventLogger()
{
LogFile = string.Format("{0}{1}", LogFilePath, FileName);
}
还有另一种方法可以抑制这些消息,而不需要任何代码更改或pragma块。使用Visual Studio -转到项目属性>构建>错误和警告>抑制警告-将1591附加到警告代码列表。
Jon Skeet的回答非常适用于使用VisualStudio进行构建。但是,如果您通过命令行构建sln(在我的例子中是通过Ant),那么您可能会发现msbuild忽略sln抑制请求。
添加到msbuild命令行解决了我的问题:
/p:NoWarn=1591
一个非常简单的方法是在.csproj文件中添加一个属性:
<Project>
<PropertyGroup>
...
<!--disable missing comment warning-->
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
...
在这里的后面,但是这个线程中的许多解决方案都专注于在项目或类中完全删除警告。
如果你想保留合法的警告,但删除一些-例如,当你使用swagger公开API时,WebApi控制器上的cancellationToken (API用户不需要这个-它是由DI提供的)。
很难看,但至少在这种情况下,取消令牌是最后一个参数。
/// <summary>
/// Creates a Service
/// </summary>
/// <param name="service">The Service Definition</param> (**note no cancellation token param**)
/// <returns>A newly created item</returns>
/// <response code="201">Returns the newly created service</response>
/// <response code="400">If there are validation errors with the submitted json body</response>
/// <response code="409">Conflict. The service already exists</response>
/// <response code="500">Because life is never perfect</response>
[ProducesResponseType(typeof(Service), 201)]
[ProducesResponseType(400)]
[ProducesResponseType(409)]
[ProducesResponseType(500)]
[HttpPost]
public async Task<ActionResult> ServiceCreate([FromBody] ServicePostRequest service,
#pragma warning disable 1573
CancellationToken cancellationToken = default) //**note: no warning**
#pragma warning restore 1573
{
来自@JonSkeet的答案几乎完成了。如果您想为解决方案中的每个项目禁用它,可以将下面的行添加到.editorconfig文件中。
dotnet_diagnostic.CS1591.severity = none
https://github.com/dotnet/roslyn/issues/41171#issuecomment-577811906
https://learn.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2022
查看文件层次结构和优先级添加文件的位置:
https://learn.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2019#file-hierarchy-and-precedence