简体   繁体   English

我的代码有数百个重复相等检查的实例 - 我该如何简化呢?

[英]My code has hundreds of instances of repeated equality checks - how can I simplify this?

The problem looks like I've had about 500 files with if statements looking like the one I have below.问题看起来像我有大约 500 个文件,其中if语句看起来像我下面的那个。 How might I replace these if statements without manually overwriting them each by each?如何在不手动覆盖它们的情况下替换这些if语句?

The if statement below returns a null reference exception if _azon is null so that is the main reason behind the replacement of the if statements.如果 _azon 为 null,则下面的if语句将返回 null 引用异常,因此这是替换 if 语句的主要原因。

 string _azon;
 public string azon
 {
     get { return _azon; }
     set {
         if (((_azon == null) && (value != null)) || !_azon.Equals(value)) {
              _azon = value;
              NotifyPropertyChanged();
          }
      }
  }

Based on your question and the code snippet in one of your comments, my understanding is that you want to replace this:根据您的问题和您的评论之一中的代码片段,我的理解是您想要替换它:

if (((_azon == null) && (value != null)) || !_azon.Equals(value)) {

with this:有了这个:

if ((_azon == null) && (value != null)) {

You can do that with the Capture groups and replacement patterns feature in VisualStudio.您可以使用 VisualStudio 中的捕获组和替换模式功能来做到这一点。 You need to capture your variable name and use a replacement pattern to substitute it in the replacement string.您需要捕获变量名称并使用替换模式将其替换为替换字符串。 Capturing the variable name is easy enough using the \w character class.使用\w字符 class 很容易捕获变量名称。

So your search expression would be:因此,您的搜索表达式将是:

if \(\(\((\w+) == null\) && \(value != null\)\) \|\| !\w+\.Equals\(value\)\) {

and your replacement pattern would be:你的替换模式将是:

if (($1 == null) && (value != null)) {

Note the (\w+) in the search expression captures your variable, and the $1 in the replacement pattern substitutes it into the replacement text.请注意,搜索表达式中的(\w+)捕获您的变量,替换模式中的$1将其替换为替换文本。

I find this site very useful for creating and testing regular expressions: https://regex101.com/我发现这个站点对于创建和测试正则表达式非常有用: https://regex101.com/

To do multi-file replacement in VisualStudio 2022 , you bring up the search dialog with the menu using "Edit/Find and Replace/Replace in Files" or use the Ctrl+Shift+H hotkey.在 VisualStudio 2022 中进行多文件替换,请使用“编辑/查找和替换/替换文件”或使用 Ctrl+Shift+H 热键调出带有菜单的搜索对话框。 On that dialog, you need to check the "Use regular expressions" checkbox.在该对话框中,您需要选中“使用正则表达式”复选框。

Assuming your NotifyPropertyChanged method is implemented something like this:假设您的NotifyPropertyChanged方法是这样实现的:

private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")  
{  
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}  

You could write a function that looks something like:你可以写一个看起来像这样的 function:

private void SetIfNeeded<T>(T value, ref T prop, [CallerMemberName] string propertyName = "") where T: class, IEquatable<T>
{
    if ((prop == null && value != null) || !prop.Equals(value))
    {
        prop = value;
        NotifyPropertyChanged(propertyName);
    }
}

and then consume in within your property setters this way:然后以这种方式在您的属性设置器中使用:

string _azon;
public string azon
{
    get { return _azon; }
    set {
            SetIfNeeded(value, ref _azon);
        }
    }
}

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

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