2011-05-06 76 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步