Initialize ResourceManager dynamically

后端 未结 2 1638
余生分开走
余生分开走 2020-12-20 04:09

I have this function and it works fine to get a translated value from this specific resource file called OkayMessages.

public static string GetReso         


        
2条回答
  •  盖世英雄少女心
    2020-12-20 04:24

    Read carefully this article and you'll find that you need to specify correct namespace of the resource. That's your problem. Here is working example if OkayResources.resx resides in project root folder:

    using System.Reflection;
    using System.Resources;
    using System.Web.UI;
    
    namespace WebApplication1
    {
        public partial class _Default : Page
        {
    
            public _Default()
            {
                var result = GetResourceString("OkayResources", "SomeKey");
            }
    
            private static string GetResourceString(string resourceFileName, string key)
            {
                var resourceName = "WebApplication1." + resourceFileName;
                var resourceManager = new ResourceManager(resourceName, Assembly.GetExecutingAssembly());
                return resourceManager.GetString(key);
            }
        }
    }
    

    If you put your resource file into Resources folder you'll have to update resource namespace:

    var resourceName = "WebApplication1.Resources." + resourceFileName;
    

提交回复
热议问题