For loop add image to imagelist

I clear my code trying to briefly highlight some things

Now I came across:

  ImageList.Add(test.Properties.Resources.test1);
  ImageList.Add(test.Properties.Resources.test2);
  ImageList.Add(test.Properties.Resources.test3);
  ImageList.Add(test.Properties.Resources.test4);
  ImageList.Add(test.Properties.Resources.test5);

(15 of them) I wonder if this can be reduced with a for loop

Sort of:

for(int i=1; i<=15; i++)
        ImageList.Add(test.Properties.Resources.test +i);

Now, of course, this will not work, but I do not know how to do it (if possible)

+4
source share
4 answers

You can iterate over resources through this code

using System.Collections;
using System.Globalization;
using System.Resources;

...

ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
    string resourceKey = entry.Key;
    object resource = entry.Value;
}
+1
source

You can use reflection to get the values:

public class Something
{
    public int Test1 { get; set; }
    public int Test2 { get; set; }
    public int Test3 { get; set; }
    public int Test4 { get; set; }
}


var thing = new Something();

var imageProperties = typeof(Something)
    .GetProperties()
    .Where(p => p.Name.StartsWith("Test"));

var imagesToAdd = imageProperties
    .Select(property => property.GetValue(thing))
    .ToList();
+1
source

IEnumerable<Image> Resources object

public IEnumerable<Image> Images
{
    get
    {
        yield return test1;
        yield return test2;
        yield return test3;
        yield return test4;
        yield return test5;
        ...
    }
}

ImageList

foreach(var image in test.Properties.Resources.Images)
{
     ImageList.Add(image); 
}
0

, # Flee. , #, , JavaScript, .

http://flee.codeplex.com/

-2

Source: https://habr.com/ru/post/1535745/


All Articles