如何强制 Resharper 使用自动生成代码的 CLR 类型名称?
根据一般命名约定,CLR 类型名称的使用(例如 String、
Int16
)应优先于使用语言特定类型名称(string
、short
)。我通常会遵循这个建议。然而,Resharper 在生成代码时似乎使用特定于语言的类型名称(重构,如 extract 方法、生成 foreach 循环等),这非常烦人。
如何强制 Resharper 使用 CLR 类型名称?
更新 许多人想知道为什么有人会强制使用 Int32
而不是 int
或 String
而不是 string
的样式,目的是更好的语法突出显示:int 呈现为关键字,Int32 呈现为类型。由于修改突出显示实现似乎有些过分,因此强制执行 CLR 类型就可以完成这项工作。这就是为什么这是我们风格指南的一部分的原因之一。
According to the General Naming Conventions the usage of CLR type names (e.g. String
, Int16
) should be preferred over the usage of language specific type names (string
, short
). I usually follow this advice. Resharper however seems to use the language specific type names when generating code (refactorings like extract method, generating foreach loops, etc.) which is very annoying.
How can I force Resharper to use the CLR type names instead?
Update
As many are wondering why someone would enforce a style where Int32
instead of int
or String
instead of string
is used, the intention was better syntax highlighting: int is rendered as a keyword, Int32 is rendered as a type. As modifying the highlighting implementation seems overkill, enforcing CLR type simply does the job. That one of the reasons why this is part of our style guide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
打开 R# 选项,转到 C# 代码样式。或者更详细地说:
RESHARPER
->Options
代码编辑
->C#
->语法样式
内置类型
在局部变量、成员和参数中,首选
-CLR 类型名称
要限定的成员< /code> -
CLR 类型name
旁白:您可能只想保护它
Team Shared
,因为大多数公共项目更喜欢关键字(并且您不想通过建议具有该样式的补丁来意外地惹恼 OSS 贡献者) 。旧版本
代码编辑
->C#
->代码风格
内置类型命名< /code>->
引用内置类型时,更喜欢使用
选择CLR 类型名称
此功能需要 R#9.1 或更高版本。
如果您必须使用更旧版本的 ReSharper
R#4.5-5.1 的扩展正是出于此目的。它的名称为 Neovolve。不幸的是,这个扩展没有移植到任何更高版本的 R# 中。
VS 2015
另外,对于 Visual Studio 2015,您可能希望在
Tools
-> 下禁用首选固有预定义类型(这会导致符号在文本编辑器中变灰) ;选项
文本编辑器
->C#
->代码风格
->首选内在预定义输入关键字*
Open R# Options, go to C# Code Style. Or in more detail:
RESHARPER
->Options
Code Editing
->C#
->Syntax Style
Build-in types
In locals, members and parameters, prefer
-CLR type name
Members to qualify
-CLR type name
Aside: You probably want to safe it
Team Shared
only as most public projects prefer keywords instead (and you don't want to accidentally annoy OSS contributors by suggesting a patch with that style).older versions
Code Editing
->C#
->Code Style
Build-in type naming
->When referencing build-in type, prefer using
chooseCLR type name
This feature requires R#9.1 or higher.
If you are bound to even older versions of ReSharper
There was an Extension for R#4.5-5.1 with exactly that purpose in mind. It's called Neovolve. Unfortunately this extension wasn't ported to any later R# versions.
VS 2015
Also for Visual Studio 2015 you may want to disable to prefer the intrinsic predefined types (which causes the symbols to be grayed out in the text editor) under
Tools
->Options
Text Editor
->C#
->Code Style
->Prefer intrinsic predefined type keyword*
这些准则指的是 api 上的公共方法名称,例如 obj.ReadInt32() (可能在序列化程序中)。在代码主体(变量和参数)中,它的指定较少。就我个人而言,我更喜欢别名
double
、int
等 - 因为:Int32
(愚蠢,但可能;其中int
始终意味着global::System.Int32
)就我个人而言,我不会担心这个。 R# 没有做任何错误。
顺便说一句 - 请注意,有几个 MS 犯这个错误的例子 - 我首先想到的是
IDataReader
/DbDataReader
和GetFloat()
(应该是GetSingle()
)。Those guidelines are referring to public method names on your api, such as
obj.ReadInt32()
(perhaps in a serializer). In the body of a code (variables and parameters) it is less specified. Personally I prefer the aliasesdouble
,int
etc - as:Int32
(stupid, but possible; where-asint
always meansglobal::System.Int32
)using System;
Personally, I would not worry about this. R# is not doing anything wrong.
As an aside - note that there are a few examples of MS getting this wrong - one that leaps to mind is
IDataReader
/DbDataReader
withGetFloat()
(should beGetSingle()
).目前,ReSharper 没有任何选项可以优先选择
System.Int16
而不是short
等。您可以投票支持 YouTrack 上的开放功能请求;但请注意,它已经开放了五年多而没有实施,您可以选择将其作为一个提示。There is currently no ReSharper option to prefer
System.Int16
overshort
, and similar. There is an open feature request on YouTrack which you can vote for; but note that it has been open for over five years without being implemented, which you may choose to take as a hint.我知道这个问题已经得到了解答,但对于那些想知道未来首选样式的人来说,.NET 团队现在正式建议使用语言名称而不是 CLR 名称,这就是 .NET Core 开发所使用的样式。
https://github.com/dotnet/ corefx/blob/master/Documentation/coding-guidelines/coding-style.md
I know this question has already been answered, but for those wondering about the preferred style in the future, the .NET team now officially recommends using the language name instead of the CLR name, and this is the style used for .NET Core development.
https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md