0

i have a static class named CommonImage which has properties of static Bitmap thats ready to get . heres my actual class:

public static class CommonImage
    {
        public static Bitmap AccountConnected { get; }

        public static Bitmap AccountDisconnected { get; }

        public static Bitmap ArrowDownIcon { get; }

        public static Bitmap ArrowUpIcon { get; }

        public static Bitmap AutoScrollIcon { get; }

        public static Bitmap RSConsDark { get; }

        public static Bitmap RSConsLight { get; }

        public static Bitmap RSDelDark { get; }

        public static Bitmap RSDelLight { get; }
    }

what i want to do:

I want to get all properties/Image that startsWith "RS" and store all image in an ImageCollection . and if possible no loop like foreach and forloop .

Elegiac
  • 361
  • 2
  • 15
  • 1
    Look into a combination of reflection and linq http://stackoverflow.com/questions/451453/how-to-get-a-static-property-with-reflection – TGH Nov 06 '14 at 05:45
  • +1 that makes a lot of sense ... thanks for the tip @TGH – Elegiac Nov 06 '14 at 05:51
  • if `CommonImage` class was created by you, why you cannot just create static method which return needed `ImageCollection`? – Fabio Nov 06 '14 at 05:58

3 Answers3

0

Try This:-

var query = typeof(CommonIcons).GetProperties().Where(x => x.Name.Contains("RS")).Select(x => x.Name).ToList();
Rahul Singh
  • 21,585
  • 6
  • 41
  • 56
  • is there a way i can save it in an imageCollection w/o looping? – Elegiac Nov 06 '14 at 05:54
  • @Elegiac - This query will return property name as List. You want image collection? – Rahul Singh Nov 06 '14 at 05:57
  • imageCollection was a control ... sorry i forgot to mention ... btw the list above returns "string" instead of image since youve select "name" i think ... how can we select the image instead? – Elegiac Nov 06 '14 at 06:00
  • @Elegiac - Yes since you mentioned 'I want to get all properties which starts with "RS"', I have fetched the property names. If you wanna fetch images then you should query `actual Data` not `definition`! – Rahul Singh Nov 06 '14 at 06:09
0

what if you try something like this..

var query = typeof(CommonImage).GetProperties().Where(x => x.Name.Contains("RS")).Select(x => x.Name).ToList();
            var ImageList = new ImageList();
            query.ForEach(propName => ImageList.Images.Add((Bitmap)typeof(CommonImage).GetProperty(propName).GetValue(typeof(CommonImage), null)));
            System.Windows.Forms.ImageList.ImageCollection col = ImageList.Images;
sm.abdullah
  • 1,777
  • 1
  • 17
  • 34
0

I wouldn't go into reflection for such a non dynamic thing, just define an extra property staticly:

public static ImageCollection RSImages
{
   get
   {
      var ic = new ImageCollection();
      ic.Add(RSConsDark);
      ic.Add(RSConsLight);
      //etc
      return ic; 
   }
}
MarkO
  • 2,143
  • 12
  • 14
  • Good Idea ..FYI.. I think ImageCollection does not have constructor like this.. talking about System.Windows.Forms.ImageList.ImageCollection – sm.abdullah Nov 06 '14 at 11:17