Results 1 to 4 of 4

Thread: Resource Files & Reflection

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    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)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width