Getting a string dynamically from strings resources

前端 未结 5 2159
鱼传尺愫
鱼传尺愫 2020-12-09 08:11

I am working on a localised C#.NET application and we are using a strings.resx file to translate hardcoded strings in the application. I use the following code

5条回答
  •  庸人自扰
    2020-12-09 08:34

    You can write a static method like this:

    public static string GetResourceTitle(string key)
    {
      ResourceManager rm = new ResourceManager(typeof(T));
      string someString = rm.GetString(key);
      return someString;
    }
    

    And call anywhere:

    var title=  GetResourceTitle<*YouResourceClass*>(key);
    

    It is useful when you want to have a generic function to get String of any Resource file.

提交回复
热议问题