【问题标题】:Convert auto property to full property将汽车属性转换为完整属性
【发布时间】:2014-04-29 15:08:49
【问题描述】:

我经常需要将自动属性转换为带有支持字段的完整属性,以便我可以实现INotifyPropertyChanged。当一个类有 50 多个属性时,它会变得非常乏味。

public string MyProperty { get; set;}

private string _myProperty;
public string MyProperty
{
    get
    {
        return _myProperty;
    }
    set
    {
        _myProperty = value;
        OnPropertyChanged("MyProperty");
    }
}

我能够创建一个代码 sn-p 以上述格式创建一个新属性,但我不知道是否可以提取现有属性的名称并键入并替换它。

我看到了kindofmagic,但我真的不想在我的项目中使用奥术魔法。

This question 解释了如何在 Resharper 中进行操作,但我没有 Resharper。我什至下载了试用版,但仍然不知道该怎么做。

有没有办法通过代码 sn-ps、宏,甚至免费扩展来做到这一点?看起来它应该相当简单。

【问题讨论】:

  • 好吧 alt+enter 没有做任何事情,因为我没有启用快捷方式,我在菜单中找不到任何有用的东西。无论如何,该解决方案只会持续 30 天。
  • Resharper 在没有启用快捷方式的情况下非常没用。下次给它一个机会。
  • 您可以使用一个名为 PropMan 的不错的简单扩展,非常快速且双向工作,marketplace.visualstudio.com/items?itemName=OlegShilo.PropMan

标签: c# visual-studio properties visual-studio-2013


【解决方案1】:

如果你有 notepad++,你可以通过 RegEx 来做(很丑,但是可​​以)

查找内容:(public)\s+([a-zA-z0-9]+)\s+([a-zA-z0-9]+)\s*\{\s*+get;\s*set;\s*\}

替换为:private \2 _\3\; \r\n \1 \2 \3 \r\n \{ \r\n get \{ return _\3\; \} \r\n set \{ _\3=value\; OnPropertyChanged\(\"\3\"\)\; \} \r\n \}

确保选中“正则表达式”

查找/替换屏幕如下所示:

它来自

收件人:

编辑:感谢 Britton,这是 Visual Studio 等价物:

查找:public[^\S\r\n](.+)[^\S\r\n](\b(_\w+|[\w-[0-9_]]\w*)\b)[^\S\r\n]{[^\S\r\n]get;[‌​^\S\r\n]set;[^\S\r\n]}

替换:private $1 _$2;\r\npublic $1 $2 {\r\nget\r\n{\r\nreturn _$2;\r\n}\r\nset\r\n{\r\n_$2 = value; OnPropertyChanged("$2");\r\n}\r\n}

【讨论】:

  • 也许同样的正则表达式也适用于 Visual Studio 自己的查找/替换?语法可能略有不同,但应该可以。
  • 不是最方便的,但它确实有效。如果没有人提出更好的答案,我会接受它作为答案。我至少能够为 Visual Studio 转换正则表达式。查找:public[^\S\r\n](.+)[^\S\r\n](\b(_\w+|[\w-[0-9_]]\w*)\b)[^\S\r\n]{[^\S\r\n]get;[^\S\r\n]set;[^\S\r\n]} 替换:private $1 _$2;\r\npublic $1 $2 {\r\nget\r\n{\r\nreturn _$2;\r\n}\r\nset\r\n{\r\n_$2 = value; OnPropertyChanged("$2");\r\n}\r\n}
  • Dan 的正则表达式查找和替换不会选择类型中具有泛型的属性,例如 public IList<Event> Events { get; set; } 或可空值类型,例如 public int? ... 。尖括号和问号把它扔掉了。一个小的修改修复了:(public)\s+([a-zA-z0-9<>\?]+)\s+([a-zA-z0-9]+)\s*\{\s*+get;\s*set;\s*\}。 (唯一的变化是添加了尖括号和问号,从(public)\s+([a-zA-z0-9]+)\s+...(public)\s+([a-zA-z0-9<>\?]+)\s+...。)
  • 另一个变化,处理方括号,例如public byte[] ...。现在第一个正则表达式变为:(public)\s+([a-zA-z0-9<>\[\]\?]+)\s+([a-zA-z0-9]+)\s*\{\s*+get;\s*set;\s*\}。 Dan 正则表达式的唯一变化是添加了<>\[\]\?
  • 亲爱的主,这个答案......道具!
【解决方案2】:

我最终使用Dan's regex 一次转换许多属性。我还发现了一个名为 PropMan 的 Visual Studio 扩展,它非常适合一次转换单个属性。只需将光标放在属性上,然后点击Ctrl+\, Ctrl+\ 即可在自动/完全之间进行转换。

【讨论】:

    【解决方案3】:

    不幸的是,Visual Studio 似乎不支持这些类型的自定义重构 sn-ps。翻阅我发现this的文档:

    SurroundsWith:允许将代码 sn-p 放置在选定的周围 一段代码。

    扩展:允许在光标处插入代码sn-p。

    重构:指定在 Visual C# 期间使用代码 sn-p 重构。 无法在自定义代码 sn-ps 中使用重构。

    我还继续创建了一个用于从头开始创建完整属性的 sn-p,但它仅适用于新属性,而不是现有属性。

    <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets
      xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <Title>fprop</Title>
          <Shortcut>fprop</Shortcut>
          <Description>Full Property with Backing Store</Description>
          <Author>Nathan McGinn</Author>
          <SnippetTypes>
            <SnippetType>Refactoring</SnippetType>
          </SnippetTypes>
        </Header>
        <Snippet>
          <Declarations>
            <Literal>
              <ID>type</ID>
              <Default>string</Default>
              <ToolTip>The type of your property</ToolTip>
            </Literal>
            <Literal>
              <ID>back</ID>
              <Default>_myProperty</Default>
              <ToolTip>The name of your backing variable</ToolTip>
            </Literal>
            <Literal>
              <ID>prop</ID>
              <Default>MyProperty</Default>
              <ToolTip>Your public property's name</ToolTip>
            </Literal>
          </Declarations>
          <Code Language="CSharp">
            <![CDATA[private $type$ $back$;
            public $type$ $prop$
            {
              get
              {
                return this.$back$;
              }
              set
              {
                this.$back$ = value;
                OnPropertyChanged("$prop$");
              }
            }]]>
          </Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>
    

    我认为就 sn-ps 而言,将需要 Visual Studio 扩展来重构现有代码(或按照 Dan 的建议进行正则表达式查找/替换)。

    【讨论】:

      【解决方案4】:

      我仅使用class1 测试了此代码。但是,它会给你一个起点。我没有对变量使用花哨的命名。请酌情更改变量名称。它会将private... 放在属性前面并忽略方法。

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      namespace ConsoleApplication1
      {
       public class Class1
       {        
          public int Val
          {
              get;
              set;
          }
      
          public int Val1
          {
              get;
              set;
          }
      
          public void M1() { }
        }
      }
      

      更改上述类的代码

      public class Program
      {
      
          public static void Main(string[] args)
          {
      
              Class1 c = new Class1();
              Type testType = c.GetType();
              PropertyInfo[] prinfo = testType.GetProperties();
              string[] filetest=File.ReadAllLines("Class1.cs"); //put correct file path here
              int index=0,i=0;            
              foreach (PropertyInfo p in prinfo)
              {                
                  while(i < filetest.Length )
                  {
                      index = filetest[i].IndexOf(p.Name);
                      if (index > 0)
                      {
                          index = 0;
                          filetest[i]=filetest[i].Insert(0, "private " + p.PropertyType.ToString() + " _" + p.Name+";" + Environment.NewLine);
                      }
                      i++;
                  }                  
      
              }
              File.WriteAllLines("Class1.cs", filetest);//put correct file path here
              Console.Read();
          }        
      }
      

      【讨论】:

        猜你喜欢
        • 2012-05-30
        • 1970-01-01
        • 1970-01-01
        • 2014-11-12
        • 2016-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多