Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor with variable parameters on class derived from Attribute does not work

I want to store additional information in my Enum values and therefore came up with Attributes. Since I want a single property to carry 1..n strings I tried to make the attribute constructor accept a variable parameter. Like this:

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

    FileTypeAttribute(params string[] extensions)
    {
        this.Extensions = extensions;
    }
}

My problem is that when I am now trying to make use of my property my compiler complains and leaves with the following error message which I really do not understand:

public enum EFileType
{
    [FileTypeAttribute("txt")]
    TEXTFILE,
    [FileTypeAttribute("jpg", "png")]
    PICTURE
}

Gives me:

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

Could anyone tell me please why this happens?

As far as I remember there is not really a possibility to make enums a little more "java'ish". But if I am missing any alternative I would be glad to hear about it.

like image 300
pdresselhaus Avatar asked Jun 19 '11 16:06

pdresselhaus


1 Answers

The constructor is implicitly private - explicitly mark it public:

public FileTypeAttribute(params string[] extensions)
{
    this.Extensions = extensions;
}
like image 65
Grant Thomas Avatar answered Oct 04 '22 04:10

Grant Thomas