0

假设我有一些类似的对象,具有很多属性:

public class SomeObject
{
  public SomeOtherObject1 Property1 { get; set; }
  public SomeOtherObject2 Property2 { get; set; }
  public SomeOtherObject3 Property3 { get; set; }
  public SomeOtherObject4 Property4 { get; set; }
  public SomeOtherObject5 Property5 { get; set; }
  public SomeOtherObject6 Property6 { get; set; }
}

如果我可以创建一个构造函数并将属性复制到构造函数中,那将是非常酷的......

public class SomeObject
{
  public SomeObject
  {
    public SomeOtherObject1 Property1 { get; set; }
    public SomeOtherObject2 Property2 { get; set; }
    public SomeOtherObject3 Property3 { get; set; }
    public SomeOtherObject4 Property4 { get; set; }
    public SomeOtherObject5 Property5 { get; set; }
    public SomeOtherObject6 Property6 { get; set; }
  }

  public SomeOtherObject1 Property1 { get; set; }
  public SomeOtherObject2 Property2 { get; set; }
  public SomeOtherObject3 Property3 { get; set; }
  public SomeOtherObject4 Property4 { get; set; }
  public SomeOtherObject5 Property5 { get; set; }
  public SomeOtherObject6 Property6 { get; set; }
}

并使用Visual Studio 的 Find And Replace with Regex将构造函数中突出显示的行从:

    public SomeOtherObject1 Property1 { get; set; }
    public SomeOtherObject2 Property2 { get; set; }
    public SomeOtherObject3 Property3 { get; set; }
    public SomeOtherObject4 Property4 { get; set; }
    public SomeOtherObject5 Property5 { get; set; }
    public SomeOtherObject6 Property6 { get; set; }

至:

    this.Property1 = new SomeOtherObject1();
    this.Property2 = new SomeOtherObject2();
    this.Property3 = new SomeOtherObject3();
    this.Property4 = new SomeOtherObject4();
    this.Property5 = new SomeOtherObject5();
    this.Property6 = new SomeOtherObject6();

首先我试过:

公共\s{:i}\s{:i}\s{\sget;\sset;\s}

这。\2 = 新 \1();

然后我想可能是线路问题,所以我尝试了:

^\s*public\s{:i}\s{:i}\s{\sget;\sset;\s}.$

这。\2 = 新 \1();

其他人对如何使它起作用有任何想法吗?

4

1 回答 1

1

您需要避开 get; 周围的 {} 放;。另外,我使用 :b 而不是 \s 并允许多个。这里:

public:b+{:i}:b+{:i}:b*\{:b*get;:b*set;:b*\}

正如你所写:

this.\2 = new \1();
于 2012-04-23T16:49:14.490 回答