I try to convert System.Drawing.SystemIcons to System.Windows.Forms.ImageList for using it in a ListView
I try the following code :
ImageList iconlist = new ImageList();
PropertyInfo[] properties = typeof(SystemIcons).GetProperties();
foreach (PropertyInfo property in properties)
{
Debug.WriteLine(property.Name);
iconlist.Images.Add(property.Name, property.GetValue(SystemIcons).ToBitmap());
}
But it’s return : ‘SystemIcons’ is a type, which is not valid in the given context
>Solution :
When you want to get the value of a static property, you have to pass null as the instance. Additionally, GetValue() returns an object, so you have to cast it:
iconlist.Images.Add(property.Name, (Icon)property.GetValue(null));
Thankfully, ImageCollection.Add() already has an overload that takes an icon, so no need for ToBitmap().