返回静态类的所有选定属性值
本文关键字:属性 静态类 返回 | 更新日期: 2023-09-27 18:29:31
我有一个名为CommonImage的静态类,它具有静态位图的属性,可以随时获取。这是我的实际班级:
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; }
}
我想做什么:
我想获取以"RS"启动的所有属性/图像,并将所有图像存储在ImageCollection
中。如果可能的话,没有像foreach和forloop那样的循环。
试试这个:-
var query = typeof(CommonIcons).GetProperties().Where(x => x.Name.Contains("RS")).Select(x => x.Name).ToList();
如果你尝试这样的东西会怎么样。。
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;
我不会对这样一个非动态的东西进行反思,只需静态地定义一个额外的属性:
public static ImageCollection RSImages
{
get
{
var ic = new ImageCollection();
ic.Add(RSConsDark);
ic.Add(RSConsLight);
//etc
return ic;
}
}