|
-
Nov 29th, 2006, 08:47 PM
#1
Thread Starter
Lively Member
Resource Files & Reflection
Hello,
I'm trying to build an image library DLL that will contain a bunch of common images that we use in many applications we distribute to customers. I have been trying to use reflection to grab the icons out of the Resource files, which I can do using this code:
Code:
public Image GetIcon(string key, string category)
{
Assembly myAssembly;
myAssembly = this.GetType().Assembly;
ResourceManager manager = new System.Resources.ResourceManager("IconLibrary." + category, myAssembly);
return (Image)manager.GetObject(key);
}
I can't seem to figure out how to list every resource in a base resource file though. I want to create a List<Image> object and return that value so that we can implement it easily into an ImageComboBox. I think I have to use a ResourceSet, but I'm not sure. Here is code I have so far, but I don't know where to go from here:
Code:
public List<Image> GetIconList(string category)
{
List<Image> retval = new List<Image>();
Assembly myAssembly = this.GetType().Assembly;
ResourceManager manager = new ResourceManager("IconLibrary." + category, myAssembly);
ResourceSet ResSet = manager.GetResourceSet(new System.Globalization.CultureInfo("en-US"), false, false);
// magic happens here ?
return retval;
}
Any help would be greatly appreciated. Also, I am using these pages from MSDN for reference:
http://msdn2.microsoft.com/en-us/lib...08(VS.71).aspx
http://msdn2.microsoft.com/en-us/lib...et(VS.71).aspx
(and links from those pages to other dependency classes)
-
Nov 29th, 2006, 09:19 PM
#2
Re: Resource Files & Reflection
The ResourceSet class implements IEnumerable, which means that you can use it in a foreach loop. I've never used it but I believe that the objects returned will be DictionaryEntry instances, where the Key property will return the name while the Value property will return the resource itself.
-
Nov 30th, 2006, 05:02 PM
#3
Thread Starter
Lively Member
Re: Resource Files & Reflection
My ResourceSet always comes up as null. Are there any obvious issues with this code:
Code:
public List<Image> GetIconList(string category)
{
List<Image> retval = new List<Image>();
Assembly myAssembly = this.GetType().Assembly;
ResourceManager manager = new ResourceManager("IconLibrary." + category, myAssembly);
CultureInfo culture = new CultureInfo("en");
ResourceSet ResSet = manager.GetResourceSet(culture, false, false);
foreach (DictionaryEntry de in ResSet)
{
retval.Add((Image)de.Value);
}
return retval;
}
-
Nov 30th, 2006, 05:13 PM
#4
Re: Resource Files & Reflection
GetResourceSet gets resources for a specific culture. Are you sure that the resources in your assembly have been marked for that culture?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|