2011-06-30 39 views
13

我需要編寫一個程序,該程序從dll讀取所有字符串資源並將它們插入某個表中。我有方法,讀取資源:從資源管理器獲取所有字符串

private static IEnumerable<KeyValuePair<string,string>> getAllResources(ResourceManager resourceManager, 
     Language language) 
    { 

     ResourceSet resourceSet = resourceManager.GetResourceSet(getCulture(language), true, true); 

     IDictionaryEnumerator dictNumerator = resourceSet.GetEnumerator(); 

     // Get all string resources 
     while (dictNumerator.MoveNext()) 
     { 
      // Only string resources 
      if (dictNumerator.Value is string) 
      { 
       var key = (string)dictNumerator.Key; 
       var value = (string)dictNumerator.Value; 
       yield return new KeyValuePair<string, string>(key, value); 
      } 
     } 
    } 

但是當我開始使用它,我注意到,它也讀出資源,像文件添加(讀取文件內容)

我怎能無視作爲「文件」添加的資源,以及只讀字符串?

+0

難道不可能在值上使用'as'鑄造嗎? –

+0

我可以使用「as」而不是「is」,但它不能解決我的問題 – Oleksey

回答

2

沒有辦法做到這一點。 例如,通過Reflector查看您的裝配資源部分。您的文本文件保存爲字符串。字符串值和文本文件值沒有區別。

然而,二進制文件不會是一個問題,對於二進制文件類型,您將byte []作爲值而不是字符串。

+0

是的,二進制文件不是問題。只有文本文件存在問題。但是當我在Visual Studio 2010中觀看資源時,它們按類型排序:「字符串」\「文件」\「圖像」\「圖標」。他們是怎麼做到的? – Oleksey

+0

這些是Visual Studio的資源表示形式。它使用文件和圖片以及圖標位置將它們作爲資源加載到程序集中。之後,您只能使用位圖,字符串或字節[]數據組裝。 – DiVan