问题描述
|
我想获取所有的Brushes,这基本上是伪代码,它将解释我正在尝试执行的操作:
For Each B in Brushes
\'Something with Brushes
End For
刷子是一种类型,那么我该怎么做呢?
解决方法
不使用反射:
Dim brushes = [Enum].GetValues(GetType(KnownColor)) _
.Cast(Of KnownColor)() _
.Where(Function(k) k >= KnownColor.Transparent AndAlso k < KnownColor.ButtonFace) _ \'//Exclude system colors
.Select(Function(k) New SolidBrush(Color.FromKnownColor(k)))
编辑(从托马斯评论)
获取颜色名称(用于画笔)
Dim brushColorNames = [Enum].GetValues(GetType(KnownColor)) _
.Cast(Of KnownColor)() _
.Where(Function(k) k >= KnownColor.Transparent AndAlso k < KnownColor.ButtonFace) _ \'//Exclude system colors
.Select(Function(k) k.ToString())
, 你能做这个吗?
dim brush as new Brush() \'needs a proper brush instance,not sure where there is one,so this line won\'t work
Dim type As Type = GetType(System.Drawing.Brushes)
Dim properties As PropertyInfo() = type.GetProperties(BindingFlags.Static)
For Each [property] As PropertyInfo In properties
Console.WriteLine(\"{0} = {1}\",[property].Name,[property].GetValue(brush,Nothing))
Next
, 这取决于您要如何使用画笔。
For Each b in GetType(Brushes).GetProperties
Dim colorName = b.Name \' If you want color names (AliceBlue through YellowGreen)
Dim brushValue = b.GetValue(Nothing,Nothing) \' Gives you a Brush
Dim brushColor = brushValue.Color \' Gives you the hex color of the brush (AliceBlue = #FFF0F8FF)
Next