gpt4 book ai didi

c# - 如何将结构中的预定义颜色转换为颜色列表?

转载 作者:太空狗 更新时间:2023-10-30 00:48:25 25 4
gpt4 key购买 nike

SkiaSharp.SKColors 结构中有一组预定义的颜色。它们作为 SKColor 类型的静态字段公开公开。

我想提取这些字段并创建一个 SKColor 列表。我的尝试如下,但我不知道在代码中指示的位置做什么。

using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Reflection;

namespace Example
{
class Program
{
static void Main()
{
Type type = typeof(SKColors);
FieldInfo[] fis = type.GetFields(BindingFlags.Static | BindingFlags.Public);
List<SKColor> colors = new List<SKColor>();

foreach(FieldInfo fi in fis)
{
//colors.Add(fi.WhatIsThis); // The point in question
}
}
}
}

这是 SKColors 的摘录:

//
// Just contains various utility colors
//
// Author:
// Miguel de Icaza
//
// Copyright 2016 Xamarin Inc
//
using System;
namespace SkiaSharp
{
public struct SKColors
{
public static SKColor Empty => new SKColor (0x00000000);
public static SKColor AliceBlue = new SKColor (0xFFF0F8FF);
public static SKColor AntiqueWhite = new SKColor (0xFFFAEBD7);
public static SKColor Aqua = new SKColor (0xFF00FFFF);
public static SKColor Aquamarine = new SKColor (0xFF7FFFD4);
public static SKColor Azure = new SKColor (0xFFF0FFFF);
public static SKColor Beige = new SKColor (0xFFF5F5DC);
public static SKColor Bisque = new SKColor (0xFFFFE4C4);
public static SKColor Black = new SKColor (0xFF000000);
public static SKColor BlanchedAlmond = new SKColor (0xFFFFEBCD);
public static SKColor Blue = new SKColor (0xFF0000FF);
public static SKColor BlueViolet = new SKColor (0xFF8A2BE2);
public static SKColor Brown = new SKColor (0xFFA52A2A);
public static SKColor BurlyWood = new SKColor (0xFFDEB887);

// trimmed for the sake of brevity
}
}

最佳答案

使用 FieldInfo,您仅保留对字段的引用,而不是其实际值。

试试这个:

var colors = typeof(SKColors)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.Select(fld => (SKColor)fld.GetValue(null))
.ToList();

参见 MSDN

关于c# - 如何将结构中的预定义颜色转换为颜色列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46890065/

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