Determine if a resource exists in ResourceManager

2019-02-21 07:48发布

Is there anyway to determine if a ResourceManager contains a named resource? Currently I am catching the MissingManifestResourceException but I hate having to use Exceptions for non-exceptional situations. There must be some way to enumerate the name value pairs of a ResourceManager through reflection, or something?

EDIT: A little more detail. The resources are not in executing assembly, however the ResourceManager is working just fine. If I try _resourceMan.GetResourceSet(_defaultCuture, false, true) I get null, whereas if I try _resourceMan.GetString("StringExists") I get a string back.

标签: c# resources
2条回答
forever°为你锁心
2楼-- · 2019-02-21 08:21

You can use the ResourceSet to do that, only it loads all the data into memory if you enumerate it. Here y'go:

    // 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);
查看更多
冷血范
3楼-- · 2019-02-21 08:37

I think you can use something like Assembly.GetManifestResourceNames to enumerate the list of resources available in the Assembly's manifest. It isn't pretty and doesn't solve all of the corner cases, but works if required.

查看更多
登录 后发表回答