在从Attribute派生的类上具有可变参数的构造方法不起作用

pdr*_*aus 5 c# attributes .net-3.5

我想在我的Enum值中存储其他信息,因此想出了属性.因为我想要一个属性来携带1..n strings我试图让属性构造函数接受一个变量参数.像这样:

[AttributeUsage(AttributeTargets.Enum, AllowMultiple = false, Inherited = false)]
public class FileTypeAttribute : Attribute
{
    public readonly string[] Extensions;

    FileTypeAttribute(params string[] extensions)
    {
        this.Extensions = extensions;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我现在尝试使用我的属性时,我的编译器会抱怨并留下以下错误消息,我真的不明白:

public enum EFileType
{
    [FileTypeAttribute("txt")]
    TEXTFILE,
    [FileTypeAttribute("jpg", "png")]
    PICTURE
}
Run Code Online (Sandbox Code Playgroud)

给我:

'FileTypeAttribute' does not contain a constructor that takes '1' arguments'FileTypeAttribute' does not contain a constructor that takes '2' arguments

谁能告诉我为什么会这样?

据我所知,实际上没有可能让枚举更加"java'ish".但如果我错过任何替代方案,我会很高兴听到它.

Gra*_*mas 9

构造函数是隐式的private- 显式标记它public:

public FileTypeAttribute(params string[] extensions)
{
    this.Extensions = extensions;
}
Run Code Online (Sandbox Code Playgroud)