确定ResourceManager中是否存在资源

Kri*_*son 17 c# resources

无论如何确定ResourceManager是否包含命名资源?目前我正在捕捉MissingManifestResourceException,但我讨厌在非特殊情况下使用Exceptions.必须有一些方法通过反射枚举ResourceManager的名称值对,或者什么?

编辑:更多细节.资源不在执行程序集中,但ResourceManager工作得很好.如果我尝试_resourceMan.GetResourceSet(_defaultCuture, false, true)我得到null,而如果我尝试_resourceMan.GetString("StringExists")我得到一个字符串.

Jon*_*son 22

您可以使用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);
Run Code Online (Sandbox Code Playgroud)