Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将自动属性转换为完整属性_C#_Visual Studio_Properties_Visual Studio 2013 - Fatal编程技术网

C# 将自动属性转换为完整属性

C# 将自动属性转换为完整属性,c#,visual-studio,properties,visual-studio-2013,C#,Visual Studio,Properties,Visual Studio 2013,我经常需要使用一个支持字段将自动属性转换为完整属性,以便实现INotifyPropertyChanged。当一个类有50多个属性时,它会变得非常乏味 public string MyProperty { get; set;} 到 我能够创建一个代码片段,以上面的格式创建一个新属性,但我不知道是否可以拉入现有属性的名称和类型并替换它 我看到了,但我真的不想在我的项目中使用奥术魔法 说明如何在Resharper中执行此操作,但我没有Resharper。我甚至下载了试用版,但仍然不知道怎么做 有没有

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

public string MyProperty { get; set;}

我能够创建一个代码片段,以上面的格式创建一个新属性,但我不知道是否可以拉入现有属性的名称和类型并替换它

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

说明如何在Resharper中执行此操作,但我没有Resharper。我甚至下载了试用版,但仍然不知道怎么做


有没有办法用代码片段、宏甚至免费扩展来实现这一点?这似乎应该相当简单

如果你有记事本++,你可以通过正则表达式来完成(很难看,但很管用)

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

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

确保选中了“正则表达式”

这是查找/替换屏幕的外观:

它是从

致:

编辑:多亏了Britton,以下是Visual Studio的等效版本:

查找:
public[^\S\r\n](.+)[^\S\r\n](\b(\uw+\124;[\ w-[0-9\u]]\ w*)\b)[^\S\r\n]{[^\S\r\n]获取[‌​^\S\r\n]set;[^\S\r\n]}


替换:
private$1\u$2\r\npublic$1$2{\r\nget\r\n{\r\nreturn\r\n}\r\nset\r\n{\r\n\u$2=value;OnPropertyChanged($2”);\r\n}\r\n}
不幸的是,Visual Studio似乎不支持这些类型的自定义重构片段。查看我发现的文档:

SurroundsWith:允许将代码段放置在选定的 一段代码

扩展:允许在光标处插入代码段

重构:指定在Visual C中使用代码段# 重构重构不能用于自定义代码段。

我还继续创建了一个用于从头创建完整属性的代码段,但它只用于新属性,而不是现有属性


fprop
fprop
带备份存储的完整属性
内森麦金酒店
重构
类型
一串
你的财产类型
返回
_我的财产
支持变量的名称
道具
我的财产
您的公共财产的名称



我认为就代码片段而言,重构现有代码(或Dan建议的正则表达式查找/替换)将需要Visual Studio扩展。

我只使用
class1
测试了这段代码。但是,它会给你一个起点。我没有对变量使用花哨的命名。请根据需要更改变量名。它将把
私有…
放在属性前面,并忽略方法

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();
    }        
}
公共类程序
{
公共静态void Main(字符串[]args)
{
Class1 c=新的Class1();
类型testType=c.GetType();
PropertyInfo[]prinfo=testType.GetProperties();
string[]filetest=File.ReadAllLines(“Class1.cs”);//在此处放置正确的文件路径
int指数=0,i=0;
foreach(prinfo中的属性infop)
{                
while(i0)
{
指数=0;
filetest[i]=filetest[i]。插入(0,“private”+p.PropertyType.ToString()+““+p.Name+”;“+Environment.NewLine);
}
i++;
}                  
}
File.writeAllines(“Class1.cs”,filetest);//在此处放置正确的文件路径
Console.Read();
}        
}

我最终使用它同时转换了许多属性。我还发现了一个名为visualstudio的扩展,它适合一次转换单个属性。只需将光标放在属性上,然后点击
Ctrl+\,Ctrl+\
在自动/完全之间进行转换。

也许同样的正则表达式也适用于Visual Studio自己的查找/替换?语法可能稍有不同,但应该可以使用。虽然不是最方便,但确实可以使用。如果没有人能想出更好的办法,我就接受这个答案。我至少能够将正则表达式转换为VisualStudio。查找:
public[^\S\r\n](.+)[^\S\r\n](\b([uw\w+.[\w-[0-9]]\w*)\b)[^\S\r\n]{[^\S\r\n]获取;[^\S\r\n]集;[^\S\r\n]}
Replace:
private$1\u$2\r\npublic$1$2{\r\nget\r\n{\r\nreturn\u$2;\r\n}\r\nset\r\n{\r\n\u$2=value;OnPropertyChanged($2”);\r\n}\r\n}
Dan的regex find and replace将不会使用类型中的泛型拾取属性,例如
公共IList事件{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)\s+([a-zA-z0-9\[\]\?]+)\s+([a-zA-z0-9]+)\s*\{\s*+get;\s*set;\s*}
。丹的正则表达式唯一的变化是增加了
\[\]\?
。亲爱的上帝,在这个答案上。。。道具!alt+enter没有做任何事情,因为我没有启用快捷方式,也不能使用f
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();
    }        
}