如何强制 Resharper 使用自动生成代码的 CLR 类型名称?

发布于 2024-12-13 19:13:57 字数 553 浏览 0 评论 0原文

根据一般命名约定,CLR 类型名称的使用(例如 String、Int16)应优先于使用语言特定类型名称(stringshort)。我通常会遵循这个建议。然而,Resharper 在生成代码时似乎使用特定于语言的类型名称(重构,如 extract 方法、生成 foreach 循环等),这非常烦人。

如何强制 Resharper 使用 CLR 类型名称?

更新 许多人想知道为什么有人会强制使用 Int32 而不是 intString 而不是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天旳寂寞 2024-12-20 19:13:57

打开 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 choose CLR 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*

只是我以为 2024-12-20 19:13:57

这些准则指的是 api 上的公共方法名称,例如 obj.ReadInt32() (可能在序列化程序中)。在代码主体(变量和参数)中,它的指定较少。就我个人而言,我更喜欢别名 doubleint 等 - 因为:

  • 更好地突出显示
  • 更少的打字
  • 可以避免非关键字使用的问题,即如果我有另一种方法/类型/属性/等称为 Int32 (愚蠢,但可能;其中 int 始终意味着 global::System.Int32
  • 即使您没有也可以工作<代码>使用System;

就我个人而言,我不会担心这个。 R# 没有做任何错误

顺便说一句 - 请注意,有几个 MS 犯这个错误的例子 - 我首先想到的是 IDataReader/DbDataReaderGetFloat()(应该是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 aliases double, int etc - as:

  • better highlighting
  • less typing
  • avoids issues with non-keyword usage, i.e. if I have another method/type/property/etc called Int32 (stupid, but possible; where-as int always means global::System.Int32)
  • works even if you don't have 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 with GetFloat() (should be GetSingle()).

零度° 2024-12-20 19:13:57

目前,ReSharper 没有任何选项可以优先选择 System.Int16 而不是 short 等。您可以投票支持 YouTrack 上的开放功能请求;但请注意,它已经开放了五年多而没有实施,您可以选择将其作为一个提示。

There is currently no ReSharper option to prefer System.Int16 over short, 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.

眼眸里的那抹悲凉 2024-12-20 19:13:57

我知道这个问题已经得到了解答,但对于那些想知道未来首选样式的人来说,.NET 团队现在正式建议使用语言名称而不是 CLR 名称,这就是 .NET Core 开发所使用的样式。

对于类型引用和方法,我们使用语言关键字而不是 BCL 类型(例如,int、string、float 而不是 Int32、String、Single 等)调用(例如 int.Parse 而不是 Int32.Parse)。

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.

We use language keywords instead of BCL types (e.g. int, string, float instead of Int32, String, Single, etc) for both type references as well as method calls (e.g. int.Parse instead of Int32.Parse).

https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文