c# - 转换 .resx 类以获取 ResourceSet

标签 c# asp.net resources casting

我正在尝试使用从 .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/

相关文章:

javascript - 全日历数据加载速度问题

c# - 在 ListView EmptyDataTemplate 中查找控件

asp.net - 非 aspx 页面的 404 重定向

java - 过滤器和 for 循环哪个是有效且更好的方法

javascript - 将资源文件数据从 Controller 传输到js文件

android - 无法从 apk/res-auto 命名空间访问项目资源

c# - 仅通过转发参数生成调用类私有(private)字段方法的方法?

c# - 不使用分发服务器的 NServiceBus F5 负载均衡订阅者

c# - 从函数返回 2 个项目

c# - 如何在移动设备或平板电脑时重定向到移动页面?