【发布时间】:2018-08-14 08:47:45
【问题描述】:
我使用的是 .NET Standard 2.0,我想创建一个带有颜色属性的自定义属性。
例如:
public class DesignAttribute : Attribute
{
public int Width { get; set; }
public ??? BackgroundColor { get; set; }
}
问题是这个属性应该是一个常量或者原始类型并且在编译时被解析。
所以它的类型不能是 System.Drawing.Color 也不能是 int 用于 ARGB 表示,因为
public class FooModel
{
[Required]
public int Id { get; set; }
[DesignAttribute(Width = 20, BackgroundColor = Color.Red.ToArgb())]
public string Name { get; set; }
}
给我一个编译错误
属性参数必须是属性参数类型的常量表达式、typeof表达式或数组创建表达式。
我还尝试使用 Byte[] 并用我的静态颜色类的 A、R、G 和 B 属性填充它,但我得到了同样的错误。即使使用 string 并将其设置为 Color.Red.Name 也不起作用。
System.Drawing.KnownColor 枚举本来是完美的,它存在于 .NET Framework 和 .NET Core 但不存在于 .NET Standard,因为 Xamarin 没有它。 (见Github thread)
所以我想知道我在 .NET Standard 中的选择是什么?如何将颜色表示为有效的属性参数?
谢谢
【问题讨论】:
标签: c# colors custom-attributes .net-standard