自动与 var 关键字交换显式类型

发布于 2024-10-06 16:30:11 字数 271 浏览 0 评论 0原文

我想自动删除所有显式类型并在一个大解决方案中与 var 关键字交换它们,例如,而不是

int a = 1;

我想要:

var a = 1;

这只是化妆品,解决方案中的代码工作得很好,我只是想让事情保持一致,因为我开始使用显式类型,但后来使用 var 关键字。

我猜我必须编写某种代码解析器 - 听起来有点麻烦。有人知道这个问题的简单解决方案吗?

干杯, 克里斯

I want to automatically remove all explicit types and exchange them with the var keyword in a big solution, e.g. instead of

int a = 1;

I want to have:

var a = 1;

This is just cosmetics, the code in the solution works perfectly fine, I just want to have things consistent, as I started out using explicit types, but later on used var-keywords.

I'm guessing I would have to write some sort of code parser - sounds a little cumbersome. Does anybody know an easy solution to this?

Cheers,
Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

栀子花开つ 2024-10-13 16:30:11

这本身并不是一个答案,但对于评论来说太长了。

您应该强烈考虑这样做。混合显式类型和推断类型不存在风格问题(您应该在需要时推断类型,无论是在使用匿名类型时还是在它使代码更易于阅读时),并且您会遇到很多潜在的问题:

  • 没有赋值的声明是不合格的
  • 分配给 null 的声明是不合格的
  • 属于超类型但初始化为子类型(或兼容但不同类型)实例的声明将改变其含义。

IE

object foo = "test";

...

foo = 2;

显然,这是一个简单(不太可能)的示例,但是将 fooobject 更改为 var 将导致 foo code> 被输入为 string 而不是 object,并且会改变代码的语义(在这种情况下它甚至不会编译,但你很容易遇到更难找到改变重载解析但产生编译时错误的场景)。

换句话说,请不要这样做。

This isn't an answer per se, but it's too long for a comment.

You should strongly consider not doing this. There's no stylistic concern with mixing explicit and inferential typing (you should infer types when you need to, either when using anonymous types or when it makes the code easier to read), and there are plenty of potential issues you'll encounter with this:

  • Declarations without assignment are ineligible
  • Declarations that are assigned to null are ineligible
  • Declarations that are of a supertype but initialized to an instance of a subtype (or compatible but different type) would change their meaning.

I.E.

object foo = "test";

...

foo = 2;

Obviously, this is a simple (and unlikely) example, but changing foo from object to var would result in foo being typed as a string instead of object, and would change the semantics of the code (it wouldn't even compile in this case, but you could easily run into more difficult to find scenarios where it changes overload resolution but doesn't produce a compile-time error).

In other words, don't do this, please.

差↓一点笑了 2024-10-13 16:30:11

首先,这可能不是一个好主意。 var 相对于 int 没有任何优势;许多声明几乎同样简单。

但如果你必须...

一个部分手动的解决方案是将 ReSharper 的“使用 var”提示变成警告并让它修复所有问题。我不知道 ReSharper 是否会全体做到这一点,但我经常用 Alt+< 的快速序列快速浏览一段做得很糟糕的第三方代码。 kbd>PgDn、Alt+Enter

这样做的一个显着优点是 ReSharper 尊重代码的语义。它不会不加区别地替换类型,而且我很确定它只会进行不影响程序含义的更改。例如:它不会替换 object o = "hello"; (我想;我不在 VS 前面检查这一点)。

Firstly, this is probably not such a good idea. There is no advantage to var over int; many declarations will be almost as simple.

But if you must...

A partly manual solution is to turn ReSharper's "Use var" hint into a warning and get it to fix them all up. I don't know if ReSharper will do it en masse, but I often rifle through a badly-done piece of third-party code with a rapid sequence of Alt+PgDn, Alt+Enter.

This has the significant advantage that ReSharper respects the semantics of your code. It won't replace types indiscriminately, and I'm pretty sure it will only make changes that don't affect the meaning of your program. E.g.: It won't replace object o = "hello"; (I think; I'm not in front of VS to check this).

中二柚 2024-10-13 16:30:11

调查莱克斯和亚克。您可以将其与 perl 或 awk 脚本结合起来以机械方式编辑源代码。

您还可以使用 CEDET 在 emacs 中执行此操作。它解析代码模块并生成代码分析表。

无论哪种情况,您都需要对描述......类声明(类名称、父类型、起点和终点)、方法声明(类似)、变量声明等的代码进行分析。然后,您将编写一些代码(perl、awk、powershell、elisp 等等)来遍历表,并对每个适当的变量声明进行替换。

Look into Lex & Yacc. You could combine that with a perl or awk script to mechanically edit your source.

You could also do this in emacs, using CEDET. It parses code modules and produces a table of its code analysis.

In either case you will need to come up with an analysis of the code that describes... class declarations (class name, parent types, start and end points), method declarations (similar), variable declarations, and so on. Then you will write some code (perl, awk, powershell, elisp, whatever) that walks the table, and does the replace on each appropriate variable declaration.

又怨 2024-10-13 16:30:11

我对以自动化方式执行此操作持谨慎态度。在某些地方,这实际上可能会改变程序的语义或引入错误。例如,

 IEnumerable<string> list = MethodThatReturnsListType();

或者

 string foo = null;
 if (!dict.TryGetValue( "bar", out foo ))
 {
      foo = "default";
 }

由于这些不是错误,因此当您出于其他原因触摸代码时,我会简单地替换它们。这样您就可以检查周围的代码并确保您没有更改语义并避免引入需要修复的错误。

I'd be wary of doing this in an automated fashion. There are places where this may actually change the semantics of the program or introduce errors. For example,

 IEnumerable<string> list = MethodThatReturnsListType();

or

 string foo = null;
 if (!dict.TryGetValue( "bar", out foo ))
 {
      foo = "default";
 }

Since these aren't errors, I would simply replace them as you touch the code for other reasons. That way you can inspect the surrounding code and make sure you aren't changing the semantics and avoid introducing errors that need to be fixed.

望喜 2024-10-13 16:30:11

Visual Studio IDE 中的搜索/替换是什么
例如,搜索 vor 'int ' 并将其替换为 'var '。

What's about search/replace in Visual Studio IDE
For example search vor 'int ' and replace it with 'var '.

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