Read a string stored in a resource file (resx) with dynamic file name

后端 未结 4 1704
感情败类
感情败类 2021-02-14 14:12

In my C# application I need to create a .resx file of strings customized for every customer.

What I want to do is avoid recompiling the entire project every time I have

4条回答
  •  余生分开走
    2021-02-14 14:32

    Use LINQ to SQL instead of referencing System.Windows.Forms.ResXResourceReader class in your project.

    public string GetStringFromDynamicResourceFile(string resxFileName, string resource)
    {
        return XDocument
                    .Load(resxFileName)
                    .Descendants()
                    .FirstOrDefault(_ => _.Attributes().Any(a => a.Value == resource))?
                    .Value;
    }
    

    And use it:

    GetStringFromDynamicResourceFile("MyFile.resx", "MyString1");
    

提交回复
热议问题