默认将课程密封
我在.editorConfig
文件中指定了许多代码样式。如果我的所有课程都可以密封
(当您创建新的)或至少它们会显示警告中,我会发现真的很有用。在.editorConfig
文件中是否有设置?
PS:请不要解释这是/不是一个好主意,因此有很多这样的线程,因此它将是多余的。
I have many code styles specified in my .editorconfig
file for my C# projects. I would find really useful if all of my classes could be sealed
by default (When you create a new one) or at least they would show warning there class is not sealed. Is there a setting for this in the .editorconfig
file ?
PS: Please dont explaint if it is/isnt a good idea, there are many such threads on SO, so it would be redundant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以编辑Visual Studio项目模板,该模板在创建新文件时使用。
我以前已经这样做了将其更改为默认值为公共类,而不是私有课程(或现在使用Visual Studio 2022)。
这是社区爱好的位置,但其他版本将相似。
C:\ Program Files \ Microsoft Visual Studio \ 2022 \ Community \ Commun7 \ Ide \ IteTemplates \ csharp \ code \ code \ code \ 1033 \ class
Notes注:这些文件是无关紧要的,例如,对于ASP.NET核心项目,因此您可能需要在周围围绕狩猎狩猎。
You could edit the visual studio item templates, which it uses when it creates a new file.
I've done this before to change it to default to public classes instead of private(or internal now with visual studio 2022).
This is the location for Community edtion, but the other editions will be similar.
C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class
Note: the files are indifferent places, for ASP.Net core projects for example so you may need to hunt around.
还有一些
.editorConfig
首选项,但是它们仅在安装之后才能工作。 meziantou.analyzer Nuget包装到您的项目中。构建解决方案后,这些密封的建议将出现。来源: https://www.meziantou.net/performance-benefits-benefits-benefits-benefits-benefits-benefits-benefits-benefits-benefits-benefits-benefits-bensou密封的class.htm
[2023] .NET 7编辑:
现在.NET 7允许您在
.editorConfig
类似的情况下指定此警告:但是它有一个要求,仅在
internal
类中起作用。 a href =“ https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1852” rel =“ nofollow noreferrer”> https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1852
There are also some
.editorconfig
preferences, but they work only after installing Meziantou.Analyzer Nuget package into your project. After you build your solution these sealed suggestions will show up.Source: https://www.meziantou.net/performance-benefits-of-sealed-class.htm
[2023] .NET 7 EDIT:
Now .NET 7 allows you to specify this warning in
.editorconfig
like:But it has one requirement, it works only on
internal
classesSource: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1852
编辑器配置文件无法处理创建新类,在VS的代码片段和模板中,您可以从该庞大的文件类型列表中选择的内容。如果您希望它们生成密封的类,则必须编辑它们或创建自己的。
您可以执行的另一个方向是编写代码分析仪和修复提供商,以使您的类
密封
,前提是您选择和实现的一组条件是正确的。这也具有向您已经书面的代码追溯应用的额外好处。The editor config file doesn't handle creating new classes, in VS it's code snippets and templates that do -- the things you select from that huge nested list of file types. If you want those to generate sealed classes, you have to either edit them or create your own.
Another direction you could go is to write a code analyzer and fix provider that makes your classes
sealed
, provided a set of conditions you get to pick and implement are true. This has the added benefit of applying retroactively to your already written code as well.