简体   繁体   English

Resharper自定义模式var

[英]Resharper custom pattern var

The code inspection of Resharper suggests to use var in C# instead of explicit type pretty much everywhere. Resharper的代码检查建议在任何地方使用C#而不是显式类型的var。 I don't like that option because too much var makes things unclear so I've disabled that option. 我不喜欢这个选项,因为太多的var会让事情变得不清楚,所以我禁用了那个选项。 However where i do like to use var is in cases of initializations with two times the type on the same line with generics (so in similar situations as the diamond operator from java 7), like: 但是我喜欢使用var的情况是在初始化的情况下使用泛型的同一行上的类型的两倍(因此在与java 7中的菱形运算符类似的情况下),如:

Dictionary<string string> dic = new Dictionary<string, string>();
// I want a suggestion to replace this to
var dic = new Dictionary<string, string>();
// but I don't want to replace things like this:
Person p = new Person();

I made a custom pattern in Resharper: 我在Resharper中制作了一个自定义模式:

Dictionary<$type1$, $type2$> $id$ = new Dictionary<$type1$, $type2$>();

with a replace to: 替换为:

var $id$ = new Dictionary<$type1$, $type2$>();

This works fine, but the pattern also finds lines that are already converted with the rule. 这样可以正常工作,但模式还会找到已使用规则转换的行。 Why and how do I fix that? 为什么以及如何解决这个问题?

Edit: put part of the text in bold because nobody appears to read it. 编辑:将部分文本设置为粗体,因为没有人看到它。

Resharper has 2 code inspections for the 'var' keyword. Resharper对'var'关键字进行了2次代码检查。 Go to Resharper -> Options... -> Code Inspection -> Inspection Severity and change the 'Use 'var' keyword when possible' to a lower severity. 转到Resharper - > Options... - > Code Inspection - > Inspection Severity并在可能的情况下将'使用'var'关键字更改为较低的严重性。 (Note, on the Inspection Severity screen you can search for words in the text box at the top) (注意,在“检查严重性”屏幕上,您可以在顶部的文本框中搜索单词)

在此输入图像描述

  1. “Resharper” menu “Resharper”菜单
  2. “Options" “选项”
  3. “Code Inspection”, then from “Inspection Severity”, find "Use Var keyword when possible” and set to “Do not show” “代码检查”,然后从“检查严重性”,找到“尽可能使用Var关键字”并设置为“不显示”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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