编程语言
首页 > 编程语言> > c# – 如何将结构中的预定义颜色转换为颜色列表?

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

作者:互联网

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,net-core,skiasharp,system-reflection
来源: https://codeday.me/bug/20190527/1163098.html