自动与 var 关键字交换显式类型
我想自动删除所有显式类型并在一个大解决方案中与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这本身并不是一个答案,但对于评论来说太长了。
您应该强烈考虑不这样做。混合显式类型和推断类型不存在风格问题(您应该在需要时推断类型,无论是在使用匿名类型时还是在它使代码更易于阅读时),并且您会遇到很多潜在的问题:
null
的声明是不合格的IE
显然,这是一个简单(不太可能)的示例,但是将
foo
从object
更改为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:
null
are ineligibleI.E.
Obviously, this is a simple (and unlikely) example, but changing
foo
fromobject
tovar
would result infoo
being typed as astring
instead ofobject
, 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.
首先,这可能不是一个好主意。
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
overint
; 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).调查莱克斯和亚克。您可以将其与 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.
我对以自动化方式执行此操作持谨慎态度。在某些地方,这实际上可能会改变程序的语义或引入错误。例如,
或者
由于这些不是错误,因此当您出于其他原因触摸代码时,我会简单地替换它们。这样您就可以检查周围的代码并确保您没有更改语义并避免引入需要修复的错误。
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,
or
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.
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 '.