2010-08-24 40 views
3

示例代碼:重構將NameValueCollection和KeyValueConfigurationCollection作爲參數?

EDITED:澄清的示例。對不起,有任何困惑。

 using System.Collections.Specialized; 
     using System.Configuration; 

    ... 

     // get collection 1 

     NameValueCollection c1 = ConfigurationManager.AppSettings; 

     // get collection 2 

     ExeConfigurationFileMap map = new ExeConfigurationFileMap(); 
     map.ExeConfigFilename = "c:\\SomeConfigFile.config"; 
     Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 
     KeyValueConfigurationCollection c2 = config.AppSettings.Settings; 

     // do something with collections 

     DoSomethingWithCollection(c1); 
     DoSomethingWithCollection(c2); 
... 

    private void DoSomethingWithCollection(KeyValueConfigurationCollection c) 
    { 
     foreach(KeyValueConfigurationElement el in c) 
     { 
      string key = el.Key; 
      string val = el.Value; 
      // do something with key and value 
     } 
    } 

    private void DoSomethingWithCollection(NameValueCollection c) 
    { 
     for(int i=0; i < c.Count; i++) 
     { 
      string key = c.GetKey(i); 
      string val = c.Get(i); 
      // do something with key and value 
     } 
    } 

目前DoSomethingWithCollection的兩個版本採取任何一個的NameValueCollection或KeyValueConfigurationCollection。

有沒有更乾淨的方式做到這一點,所以只有一個版本的DoSomethingWithCollection?

謝謝。

回答

4

嗯,DoSomethingWithCollection應該接受兩個參數 - 1)ICollection/IEnumerable,它允許你枚舉所有的值2)委託給對象並給你鍵值和返回值。所以你需要基本上寫兩種不同的方法來解析這兩種類型的集合。

編輯:給出一個想法的示例代碼。

delegate void KeyValueItemParser(object item, out string key, out string value); 

void DoSomethingWithCollection(ICollection items, KeyValueItemParser parser) 
{ 

    string key, value 
    foreach(object item in items) 
    { 
     parser(item, out key, out value); 
     // do whatever you want to with key & value 
    } 
} 

編輯 2:不知道你的意思通過不需要委託 - 如果你需要有DoSomethingWithCollection的一個版本,你需要有至少一些代碼,工作在兩個不同的集合。代表是最簡單的方法。另一種方法是定義一個接口,該接口將提供NamedValuePair/KeyValuePair的集合/枚舉,然後寫入實現此接口的包裝類以用於不同類型的目標集合。模板代碼將是

interface IKeyValuePairCollection 
{ 
    int Count {get; } 
    KeyValuePair<string, string> Item(int index) { get; } 
} 

class NamedValueCollectionWrapper : IKeyValuePairCollection 
{ 

    private NamedValueCollection _inner; 

    public NamedValueCollectionWrapper(NamedValueCollection target) 
    { 
    -inner = target; 
    } 

    // roll out ur implementation to IKeyValuePairCollection 
} 

class KeyValueConfigurationCollectionWrapper : IKeyValuePairCollection 
{ 
    ... 
} 

海事組織,一個簡單的要求太多的工作。

+0

我刪除了我的編輯「不需要一個代表」的一部分 - 我誤會了你試圖做。從這裏的答覆看來,這兩種類型之間沒有足夠的共同點來做出簡單的解決方案。不過謝謝你的努力。 – 2010-08-24 07:47:33

1

您可以編寫兩個過載DoSomethingWithCollection,一個需要NameValueCollection,另一個需要KeyValueConfigurationCollection。然後,每個重載遍歷集合,將鍵和值傳遞給第三個方法,該方法執行您想要執行的任何操作。

0

至於兩個集合NameValueCollectionKeyValueConfigurationCollection同時實現ICollectionIEnumerable你可以做下一個:

private void DoSomethingWithCollection(ICollection collection) 
// private void DoSomethingWithCollection(IEnumerable collection) 
{ 
    NameValueCollection nv; 
    KeyValueConfigurationCollection kvc; 
    if ((nv = collection as NameValueCollection) != null) 
    { 
     // do stuff directly or call iterate and call 3rd method 
    } 
    else if ((kvc = collection as KeyValueConfigurationCollection) != null) 
    { 
     // do stuff directly or call iterate and call 3rd method 
    } 
} 
相關問題