简体   繁体   中英

Automatically exchange explicit type with var-keyword

I want to automatically remove all explicit types and exchange them with the var keyword in a big solution, eg 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

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.

IE

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.

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. Eg: It won't replace object o = "hello"; (I think; I'm not in front of VS to check this).

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,

 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.

关于在Visual Studio IDE中进行搜索/替换的操作,例如,搜索vor'int'并将其替换为'var'。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM