5

结构中有一组预定义的颜色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
    }
}
4

1 回答 1

7

使用FieldInfo,您只持有对该字段的引用,而不是其实际值。

试试这个:

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

MSDN

于 2017-10-23T13:10:24.510 回答