我得到这个警告:“缺少公开可见类型或成员的XML注释”。

如何解决这个问题?


当前回答

禁止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

其他回答

Jon Skeet的回答非常适用于使用VisualStudio进行构建。但是,如果您通过命令行构建sln(在我的例子中是通过Ant),那么您可能会发现msbuild忽略sln抑制请求。

添加到msbuild命令行解决了我的问题:

/p:NoWarn=1591

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指令从行首开始

当然要向公开可见的类型和成员添加XML注释:)

///<Summary>
/// Gets the answer
///</Summary>
public int MyMethod()
{
   return 42;
}

所有成员都需要这些<summary>类型的注释——这些注释也会显示在智能感知弹出菜单中。

得到此警告的原因是因为您已经将项目设置为输出文档xml文件(在项目设置中)。这对于类库(.dll程序集)很有用,这意味着.dll的用户可以在visual studio中获得API的智能感知文档。

我建议你买一份GhostDoc Visual Studio插件。使记录变得更容易。

#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

一个非常简单的方法是在.csproj文件中添加一个属性:

<Project>
    <PropertyGroup>
        ...     
        <!--disable missing comment warning-->
        <NoWarn>$(NoWarn);1591</NoWarn>
    </PropertyGroup>
...