簡體   English   中英

確定ResourceManager中是否存在資源

[英]Determine if a resource exists in ResourceManager

無論如何確定ResourceManager是否包含命名資源? 目前我正在捕捉MissingManifestResourceException,但我討厭在非特殊情況下使用Exceptions。 必須有某種方法通過反射枚舉ResourceManager的名稱值對,或者什么?

編輯 :更多細節。 資源不在執行程序集中,但ResourceManager工作得很好。 如果我嘗試_resourceMan.GetResourceSet(_defaultCuture, false, true)我得到null,而如果我嘗試_resourceMan.GetString("StringExists")我得到一個字符串。

您可以使用ResourceSet來執行此操作,只有在枚舉它時才將所有數據加載到內存中。 在這里你好:

    // At startup.
    ResourceManager mgr = Resources.ResourceManager;
    List<string> keys = new List<string>();

    ResourceSet set = mgr.GetResourceSet(CultureInfo.CurrentCulture, true, true);
    foreach (DictionaryEntry o in set)
    {
        keys.Add((string)o.Key);
    }
    mgr.ReleaseAllResources();

    Console.WriteLine(Resources.A);

我認為您可以使用類似Assembly.GetManifestResourceNames的內容來枚舉Assembly的清單中可用的資源列表。 它不漂亮,並沒有解決所有的角落情況,但如果需要的話可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM