开发者

Casting .resx classes to obtain ResourceSet

I am trying to populate a dropdownlist with data pulled from a .resx file. Instead of having 5 different functions I'd like to be able to pass in the name of the .resx file and cast it somehow so I can retrieve it using GetReourceSet.

Here's what I'm currently doing:

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();
}

In the example above I have a .resx file named CountryPickList.resx so its just a matter of using the .resx name to retrieve the ResourceSet using GetResourceSet...

What I would like to be able to do is figure out how I can pass in a string to my function with the name of the .resx and obtain the same resu开发者_高级运维lts.

Any suggestions would be greatly appreciated!

cheers,

rise4peace


ResourceManager has a constructor that takes the name of a resources file.

You can therefore write the following code:

ResourceSet rs = new ResourceManager(whatDropDownList.Name, GetType().Assembly)
                     .GetResourceSet(CultureInfo.CurrentCulture, true, true);

EDIT: In ASP.Net, it's not quite the same. You can view the generated source code (.designer.cs) for one of your resource files and look at the implementation of the static ResourceManager property.

It looks like this:

ResourceManager temp = new ResourceManager("Resources.Resource1", Assembly.Load("App_GlobalResources"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜