添加注释到Settings.Designer.cs
我在 Visual Studio 2008 项目中使用 .Settings 文件,该文件会从 PublicSettingsSingleFileGenerator 自定义自动生成 Settings.Designer.cs 文件工具。
这工作正常,但我还想在编译选项中启用“警告为错误”,以强制每个人保持 XML 注释是最新的,但我不知道如何向自动中的所有元素添加注释- 生成的代码。
通过在设计视图中选择元素并在属性窗口中添加“说明”,可以为实际属性添加注释。但似乎没有办法对类声明或默认实例属性执行此操作。
重现此问题的步骤如下
- 创建一个新项目
- 将设置文件添加到项目
- 将设置文件的“访问修饰符”设置为公共
- 转到项目属性,构建部分
- 将“将警告视为错误”设置为全部
- 检查输出XML 文档文件选项
- 构建解决方案
,这是在 PublicSettingsSingleFileGenerator Settings.Designer.cs 文件中生成的代码
namespace SettingsTest {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
public sealed partial class Settings1 : global::System.Configuration.ApplicationSettingsBase {
private static Settings1 defaultInstance = ((Settings1)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings1())));
public static Settings1 Default {
get {
return defaultInstance;
}
}
}
}
不确定此额外信息是否应该成为问题部分的一部分,因为它以某种方式指向回答
根据此链接,应用于该类的属性似乎是错误的。
“CompilerGenerateAttribute< /strong> - 此属性仅供编译器使用,并指示特定的代码元素是编译器生成的,无论如何都不应在源代码中使用。”
“GeneratedCodeAttribute - 此属性供生成代码的自定义工具使用。它只能应用于一遍又一遍地重新生成的代码,并且不应由用户期望的模板使用如果生成的类型是分部类,则它也不应该在类型级别应用。在这种情况下,它应该仅应用于包含在类型的生成部分中的各个成员。”
更新
我已在 Microsoft Connect 网站上提出了一个错误报告,并将在获得更多信息后进行更新并接受答案 http ://connect.microsoft.com/VisualStudio/feedback/details/634692/publicsettingssinglefilegenerator-code-fails-when-treat-warnings-as-errors-is-set-to-all-and-xml-documentation-is-on
I'm using a .Settings file in my Visual Studio 2008 project which auto-generates a Settings.Designer.cs file from the PublicSettingsSingleFileGenerator custom tool.
This works fine, but I also want to enable "Warnings as Error" in the compilation options, to force everyone to keep the XML comments up to date, but I don't know how to add comments to all of the elements within the auto-generated code.
The actual properties can have comments added by selecting the elements in the design view and adding a "Description" within the properties window. But there doesn't seem to be a way to do this for the class declaration, or the default instance property.
Steps to reproduce this problem are as follows
- Create a new project
- Add a Settings file to the project
- Set the "Access Modifier" of the setting file to Public
- Goto the project properties, Build section
- Set "Treat Warnings as Errors" to All
- Check the Output XML Documentation file option
- Build the Solution
and this is the code which is generated in the PublicSettingsSingleFileGenerator Settings.Designer.cs file
namespace SettingsTest {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
public sealed partial class Settings1 : global::System.Configuration.ApplicationSettingsBase {
private static Settings1 defaultInstance = ((Settings1)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings1())));
public static Settings1 Default {
get {
return defaultInstance;
}
}
}
}
Not sure if this extra info should be part of the question section, as it goes some way to pointing at an answer
According to this link the attributes applied to the class appear to be wrong.
"CompilerGenerateAttribute - This attribute is for compiler use only and indicates that a particular code element is compiler generated. This should never be used in source code whatsoever."
"GeneratedCodeAttribute - This attribute is for use by custom tools that generate code. It should only be applied to code that is re-generated over and over and should not be used by templates that the user is expected to modify. Nor should it be applied at the type level if the type being generated is a partial class. In this situation, it should be applied only against the individual members that are contained within the generated part of the type."
Update
I've raised a bug report for this on the Microsoft Connect site and will update, and accept an answer when we get some more information http://connect.microsoft.com/VisualStudio/feedback/details/634692/publicsettingssinglefilegenerator-code-fails-when-treat-warnings-as-errors-is-set-to-all-and-xml-documentation-is-on
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有好的方法可以做到这一点,您无法在自动生成的文件中注入#pragma warning禁用。顺便说一句,Winforms 设计器文件也存在问题。项目+属性,构建选项卡,抑制警告= 1591。但这也会在您可能希望打开诊断的地方禁用诊断。 #pragma 警告恢复并不能解决这个问题。
There's no good way to do this, you can't inject the #pragma warning disable in the auto-generated file. Also a problem with Winforms designer files btw. Project + Properties, Build tab, Suppress warnings = 1591. But that will disable the diagnostic also where you might want it turned on. A #pragma warning restore doesn't fix that.
不幸的是,这里存在冲突的问题\设置:
这样做的结果是您的警告被转换为无法修复的编译器错误。您无法可靠地将注释手动添加到设置类,因为下次重新生成类时它们将丢失。
考虑将您的设置放在单独的项目中,并关闭该项目上的 XML 文档。
Unfortunately there are conflicting issues\settings here:
The result of this is that you have warnings being converted to compiler errors which you cannot fix. You cannot reliably add comments to the settings class manually as they will be lost next time the class is regenerated.
Consider placing your settings in a separate project and turning off XML documentation on that project.
设置是一个部分类:创建第二个空部分并对其进行注释。
Settings is a partial class: create a second, empty part and comment it.
参阅在 Visual Studio 2010 中记录 (XML) 应用程序设置
请 每个设置的描述属性,这将反过来在生成的代码中创建 XML 注释。在 VS2010 中测试可以正常工作。
See Documenting (XML) Application Settings in Visual Studio 2010
There's a description property for every setting and this will in turn create the XML comment in the generated code. Tested in VS2010 as working.