C#:el parámetro constructor de atributo no es un tipo de parámetro de atributo válido

C#:el parámetro constructor de atributo no es un tipo de parámetro de atributo válido

Problema

Creé una clase de atributo personalizada y estoy tratando de pasar un valor. Se ve así:

public enum ComputerStatus
{
	[BgColor(Color.Yellow)]
	Unregistered,
	
	[BgColor(Color.LightGreen)]
	Registered,
	
	[BgColor(Color.Red)]
	PingFailed,
	
	[BgColor(Color.Red)]
	PortNotFound,
	
	[BgColor(Color.LightGreen)]
	FoundAndRegistered
}
Code language: C# (cs)

Recibo el siguiente mensaje de error:

También recibí este mensaje de error, que tiene la misma causa subyacente:

Solución

Debe especificar un valor que se considere constante durante el tiempo de compilación.

Estos son ejemplos de los tipos que puede pasar:

public enum CustomAttributeParameterTester
{
	[CustomAttributeValidParameters(typeof(ITestInterface))]
	Interface,

	[CustomAttributeValidParameters(typeof(Test))]
	Class,

	[CustomAttributeValidParameters(1)]
	IntegerLiteral,

	[CustomAttributeValidParameters(CustomAttributeValidParameters.ONE)]
	IntegerConstant,

	[CustomAttributeValidParameters("test string")]
	StringLiteral,

	[CustomAttributeValidParameters(1, 2, 3)]
	ParamsArray,

	[CustomAttributeValidParameters(new[] { true, false })]
	Array,

	[CustomAttributeValidParameters(TestEnum.Deployed)]
	Enum
}
public class CustomAttributeValidParameters : Attribute
{
	public const int ONE = 1;
	public CustomAttributeValidParameters(Type interfaceType)
	{

	}
	public CustomAttributeValidParameters(int i)
	{

	}
	public CustomAttributeValidParameters(string s)
	{

	}
	public CustomAttributeValidParameters(params int[] args)
	{

	}
	public CustomAttributeValidParameters(bool[] arr)
	{

	}
	public CustomAttributeValidParameters(TestEnum testEnum)
	{

	}
}
public enum TestEnum
{
	Init,
	Tested,
	Deployed
}
public interface ITestInterface
{ }
public class Test : ITestInterface
{

}
Code language: C# (cs)

Cómo lo resolví para mi situación específica

En mi caso, en lugar de usar System.Drawing.Color (que es una estructura), tengo que pasar System.Drawing.KnownColor, que es una enumeración (por lo tanto, una constante de tiempo de compilación), y luego asignarlo a Color.

BgColorAttribute:mi atributo personalizado

using System;
using System.Drawing;

namespace AttributeProblem
{
    public class BgColorAttribute : Attribute
    {
        public Color Color { get; }

        public BgColorAttribute(KnownColor c)
        {
            //Why use KnownColor? Because can't have Color, which is a struct, as the parameter to an attribute!
            Color = Color.FromKnownColor(c);

        }
    }
}
Code language: C# (cs)

ComputerStatus:donde estoy usando el atributo personalizado

using System.Drawing;

namespace AttributeProblem
{
    public enum ComputerStatus
    {
        [BgColor(KnownColor.Yellow)]
        Unregistered,
        
        [BgColor(KnownColor.LightGreen)]
        Registered,
        
        [BgColor(KnownColor.Red)]
        PingFailed,
        
        [BgColor(KnownColor.Red)]
        PortNotFound,
        
        [BgColor(KnownColor.LightGreen)]
        FoundAndRegistered
    }
}

Code language: C# (cs)