gpt4 book ai didi

c# - 遍历 System.Drawing.Color 结构并使用它来创建 System.Drawing.Pen

转载 作者:太空狗 更新时间:2023-10-29 21:22:42 28 4
gpt4 key购买 nike

我想遍历 System.Drawing.Color 结构并使用它来初始化笔列表。

我是这样试的,但是字段类型不合适:

colorList = new List<System.Drawing.Pen>();

foreach (var field in typeof(System.Drawing.Color).GetFields())
{
if (field.FieldType.Name == "Color" && field.Name != null)
{
colorList.Add(new System.Drawing.Pen(field, (float)1));
}
}

请帮帮我。

最佳答案

除了 Color.Empty ,它们是属性,而不是字段:

var colorList = new List<System.Drawing.Pen>();

foreach (var prop in typeof(System.Drawing.Color).GetProperties(BindingFlags.Public | BindingFlags.Static))
{
if (prop.PropertyType == typeof(System.Drawing.Color))
{
colorList.Add(new System.Drawing.Pen((System.Drawing.Color)prop.GetValue(null), 1f));
}
}

这会生成具有 141 种颜色的 Pen 以及我正在运行的版本,应该对应于 Color Properties 中的列表.它不返回 Empty,尽管它确实有 Transparent。此外,我将 (float)1 更改为 1ff 告诉编译器这是一个 float 文字,比将 1 转换为 float 更简洁。

关于c# - 遍历 System.Drawing.Color 结构并使用它来创建 System.Drawing.Pen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19575872/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com