Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An attribute argument must be a constant expression, ...- Create an attribute of type array

Here is my custom attribute and a class I'm using it on:

[MethodAttribute(new []{new MethodAttributeMembers(), new MethodAttributeMembers()})]
public class JN_Country
{

}

public class MethodAttribute : Attribute
{
    public MethodAttributeMembers[] MethodAttributeMembers { get; set; }

    public MethodAttribute(MethodAttributeMembers[] methodAttributeMemberses)
    {
        MethodAttributeMembers = methodAttributeMemberses;
    }
}

public class MethodAttributeMembers
{
    public string MethodName { get; set; }
    public string Method { get; set; }
    public string MethodTitle { get; set; }
}

The syntax error, displayed on the first line above:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

Why does this error occur?

like image 516
Football-Is-My-Life Avatar asked Sep 16 '14 00:09

Football-Is-My-Life


2 Answers

This supplements the information Simon already gave.

I found some documentation here: Attribute parameter types:

The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:

  • One of the following types: bool, byte, char, double, float, int, long, sbyte, short, string, uint, ulong, ushort.
  • The type object.
  • The type System.Type.
  • An enum type, provided it has public accessibility and the types in which it is nested (if any) also have public accessibility (Attribute specification).
  • Single-dimensional arrays of the above types. (emphasis added by me)

A constructor argument or public field which does not have one of these types, cannot be used as a positional or named parameter in an attribute specification.

The last bullet point explains your syntax error. You've defined a one-dimensional array, but it should only be of primitive types, string, etc. as listed in the previous bullet points.

like image 77
Grant Winney Avatar answered Nov 09 '22 01:11

Grant Winney


Attribute arguments must be compile-time constant. That means that the compiler must be able to "bake in" the values of the arguments when the assembly is compiled. new ReferenceType() is not constant - it must be evaluated at runtime to determine what it is.

Interestingly, this is a little bit flimsy in that there are some edge cases to that rule. Other than those though, you cannot do what you're trying to do.

like image 27
Simon Whitehead Avatar answered Nov 09 '22 02:11

Simon Whitehead