RSS

An attribute argument must be a constant expression, typeof expression […]

10 Jul

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

Working with attributes might be very useful, but in the case when you get such an error it might be frustrating solving it

Attribute Error Image
Error 1 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.

So what have I done? Well good question.

Well you have no other option but to thoroughly search all your attributes classes for the failing one, because you have no information where and because this error occurs.

After some tries I figured out which Attribute class was the failing one. But I had no clue why.

My code looked like following:

public class MyCustomAttribute : Attribute
{
    public MyCustomAttribute(string s1 , string s2= null)
    {}
}

and I used the attribute like follows:

[MyCustomAttribute("hello")]

In the other hand following attribute usage worked:

[MyCustomAttribute("hello","world")]

Therefore I tried to split the constructor into two and eliminated the optional parameter. Wow it worked. Therefore the solution is to not use optional parameters in Attributes.

Advertisements
 
Leave a comment

Posted by on July 10, 2011 in C-Sharp

 

Tags:

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: