确定是否在ResourceManager中存在资源

问题描述:

反正是有,以确定是否一个ResourceManager包含一个名为资源?目前,我抓住MissingManifestResourceException但我讨厌不得不使用非特殊情况下例外。 ?必须有某种方式通过反射,或一些列举一个ResourceManager的名称值对

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?

修改:更详细一点。资源是不是在执行程序集,但是,ResourceManager会工作得很好。如果我尝试 _resourceMan.GetResourceSet(_defaultCuture,假,真)我得到空,而如果我尝试 _resourceMan.GetString(StringExists)我得到一个字符串返回。

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.

您可以使用的ResourceSet要做到这一点,只有它加载所有数据到内存中,如果你列举了。这里y'go:

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);