我正在尝试使用从 .resx 文件中提取的数据填充下拉列表。我不想拥有 5 个不同的函数,而是希望能够传入 .resx 文件的名称并以某种方式转换它,以便我可以使用 GetReourceSet 检索它。
这是我目前正在做的事情:
protected void populateCountryPickList(DropDownList whatDropDownList)
{
ResourceSet rs = Resources.CountryPickList.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
whatDropDownList.DataSource = rs;
whatDropDownList.DataTextField = "Value";
whatDropDownList.DataValueField = "Key";
whatDropDownList.DataBind();
}
在上面的示例中,我有一个名为 CountryPickList.resx 的 .resx 文件,因此只需使用 .resx 名称通过 GetResourceSet 检索 ResourceSet...
我想做的是弄清楚如何将一个字符串传递给我的函数并获得相同的结果。
如有任何建议,我们将不胜感激!
干杯,
和平崛起
最佳答案
ResourceManager
有一个 constructor采用 resources
文件的名称。
因此您可以编写以下代码:
ResourceSet rs = new ResourceManager(whatDropDownList.Name, GetType().Assembly)
.GetResourceSet(CultureInfo.CurrentCulture, true, true);
编辑:在 ASP.Net 中,情况并不完全相同。您可以查看为您的某个资源文件生成的源代码 (.designer.cs
),并查看静态 ResourceManager
属性的实现。
看起来像这样:
ResourceManager temp = new ResourceManager("Resources.Resource1", Assembly.Load("App_GlobalResources"));
关于c# - 转换 .resx 类以获取 ResourceSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1665125/