2011-05-06 77 views
2

我试图将一个.cs文件中的所有硬编码字符串从常量文件中加载。用C#中的常量替换硬编码的字符串

例如

string capital="Washington"; 

应加载

string capital=Constants.capital; 

,并且将在Constants.cs加入

public final const capital="Washington"; 

我需要一个Java/C#代码片段来做到这一点我无法使用任何第三方工具。对此有何帮助?

编辑:

阅读注释和答案后,我得到一个感觉,我不clear.I只是想办法,以取代将有“”和撕裂其关闭,取而代之的是所有的硬编码常量常数。并在Constants.cs.中添加该属性。这也可以是简单的文本处理。

+2

你不能创建资源文件吗? – Jonathan 2011-05-06 05:26:24

+0

不,我必须从另一个cs文件加载它。很少开发人员而不是设计师:) – Harish 2011-05-06 05:30:02

回答

2

几个替换字符串应该让你开始的提示:

假设你的字符串处理函数被称为ProcessStrings。

1)将Constants.cs包含到与ProcessStrings函数相同的项目中,因此它将与重构代码一起编译。

2)反映了你的常量类构建语言字符串常量名的解释,是这样的:

Dictionary<String, String> constantList = new Dictionary<String, String>(); 
FieldInfo[] fields = typeof(Constants).GetFields(BindingFlags.Static | BindingFlags.Public); 
String constantValue; 

foreach (FieldInfo field in fields) 
{ 
    if (field.FieldType == typeof(String)) 
    {      
     constantValue = (string)field.GetValue(null);    
     constantList.Add(constantValue, field.Name); 
    } 
} 

3)constantList现在应该包含常量名的完整列表,由字符串索引他们代表。

4)从文件中抓取所有行(使用File.ReadAllLines)。

5)现在遍历这些行。像下面这样的东西应该允许你忽略你不应该处理的行。

//check if the line is a comment or xml comment 
if (Regex.IsMatch(lines[idx], @"^\s*//")) 
    continue; 

//check if the entry is an attribute 
if (Regex.IsMatch(lines[idx], @"^\s*\[")) 
    continue; 

//check if the line is part of a block comment (assuming a * at the start of the line) 
if (Regex.IsMatch(lines[idx], @"^\s*(/\*+|\*+)")) 
    continue; 

//check if the line has been marked as ignored 
//(this is something handy I use to mark a string to be ignored for any reason, just put //IgnoreString at the end of the line) 
if (Regex.IsMatch(lines[idx], @"//\s*IgnoreString\s*$")) 
    continue; 

6)现在,匹配线上的任何引用字符串,然后通过每个匹配并检查它的几个条件。如果需要,您可以删除其中一些条件。

MatchCollection mC = Regex.Matches(lines[idx], "@?\"([^\"]+)\""); 

foreach (Match m in mC) 
{       

    if (   
     // Detect format insertion markers that are on their own and ignore them, 
     !Regex.IsMatch(m.Value, @"""\s*\{\d(:\d+)?\}\s*""") && 
     //or check for strings of single character length that are not proper characters (-, /, etc) 
     !Regex.IsMatch(m.Value, @"""\s*\\?[^\w]\s*""") && 
     //check for digit only strings, allowing for decimal places and an optional percentage or multiplier indicator 
     !Regex.IsMatch(m.Value, @"""[\d.]+[%|x]?""") && 
     //check for array indexers 
     !(m.Index <= lines[idx].Length && lines[idx][m.Index - 1] == '[' && lines[idx][m.Index + m.Length] == ']') &&   
     ) 
    { 
     String toCheck = m.Groups[1].Value; 

     //look up the string we found in our list of constants 
     if (constantList.ContainsKey(toCheck)) 
     { 
      String replaceString; 

      replaceString = "Constants." + constants[toCheck];    

      //replace the line in the file 
      lines[idx] = lines[idx].Replace("\"" + m.Groups[1].Value + "\"", replaceString); 
     } 
     else 
     { 

      //See Point 8.... 

     } 
    } 

7)现在加入备份行数组,并将其写回文件。这应该会让你获得最大的成功。

8)为了让它产生字符串的常量,你还没有一个条目,在else块中查找字符串, 为字符串中的常量生成一个名字(我刚刚删除了所有特殊的字符和空格,并将其限制为10个字)。然后使用该名称和原始字符串(来自点6中的toCheck变量)进行常量声明并将其插入到Constants.cs中。 然后,当你再次运行该函数时,将使用这些新的常量。

+0

辉煌,你rock.Have只有一票,爱有投票机器人:) – Harish 2011-05-06 17:02:43

0

是否有一个原因,你不能把它们放到一个静态类或只是在你的应用程序中的文件?你可以将常量放在任何地方,只要它们的范围适当,你可以从任何地方访问它们。

+1

我认为OP想要自动化这种重构的过程,并且正在寻找一些代码来完成这项工作。我认为他/她只需要亲自完成工作。 – tzup 2011-05-06 05:33:06

0
public const string capital = "Washington"; 

如果const不会在静态类的工作,那么这将是

public static readonly string capital = "Washington"; 
0

,如果你真的想这样做,你所描述的方式,用一个StreamReader,由\拆分读取文件r \ n,检查第一件事是否是“字符串”,然后在该字符串元素上进行所有替换... 确保每次更改该字符串声明时,都会将该附加行添加到另一个文件中。

0

您可以为常量创建类项目,或者如果您有助手类项目,则可以为常量添加新类(Constants.cs)。

public static class Constants 
{ 
    public const string CAPITAL_Washington = "Washington"; 
} 

现在,您可以使用此:

string capital = Constants.CAPITAL_Washington; 

你不妨命名常量相当具体。

1

我不知道是否有任何这样的代码可用,但我提供了一些指导方针如何实施。

  1. 你可以写一个宏/独立的应用程序(我觉得宏是一个更好的选择)
  2. 解析当前文档或项目/解决方案中的所有文件
  3. 写一个正则表达式查找的字符串(那么XAML中的字符串呢?)。 [string]([az A-Z0-9] )[“]([az A-Z0-9])[”] [;] - 这是无效的,我只是提供讨论
  4. 从代码中提取常量。
  5. 检查相似的弦已经存在于您的静态类
  6. 如果没有找到,插入静态类
  7. 新条目变量名称
  8. 转到第2步