正则表达式替换 - 是否有更好的方法来切换声明和赋值

时间:2011-02-02 11:16:48

标签: c# regex visual-studio-2010

我正在寻找一种切换我的作业的方法,即:

a = b;

变为

b = a;

如果有人想知道,那就是加载设置并卸载它们。

我为它制作了一个正则表达式: 找到:{[^:b]*} = {[^;]*} 替换为:\2 = \1

这样可以正常工作,但还有其他方法可以加载和保存我缺少的设置吗?

1 个答案:

答案 0 :(得分:0)

另一种方法:如何使用复制值的方法创建设置类?这样,您只需编写一次作业列表:

using System;

class Settings {
    public int ValueA { get; set; }
    public string ValueB { get; set; }
    public void CopySettings(Settings other) {
        ValueA = other.ValueA;
        ValueB = other.ValueB;
    }
}

class Program {
    static void Main(string[] args) {
        Settings a = new Settings() { ValueA = 3, ValueB = "something" };
        Settings b = new Settings();
        // and then you can do one the following, which will copy all settings
        // in both cases...
        b.CopySettings(a);
        a.CopySettings(b);
    }
}