11

I'm getting this error,

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

When I try to write something like this

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class UrlAttribute : Attribute
{
    public UrlAttribute(string pattern, string name=null)
    {
        // ...

It doesn't even show a line number, but it disappears when I take out that =null bit.

Actually, the error only occurs both when I provide a default value and rely on it (i.e., I omit it) like so

    [Url("/index")]

I'm curious to know why this? How is "null" not a constant expression?

5
  • 1
    I received feedback from Microsoft, and updated my response accordingly. Commented Oct 8, 2010 at 18:24
  • 1
    @kbrimington: Thanks for the update! Sounds like we'll have to wait before the fix is released... oh well, it's hardly critical :) Commented Oct 9, 2010 at 2:36
  • object name=null also gives this error. Commented Jan 23, 2012 at 13:43
  • 1
    As of Visual Studio 2012 Update 3, with a .net 4.0 project being built - this problem still exists. Commented Jan 30, 2014 at 4:13
  • 1
    This bug is still present in VS 2013 Commented Mar 28, 2015 at 17:21

2 Answers 2

9

I'm calling 'bug'.

I hope you don't mind, I reported the bug to Microsoft.

UPDATE:

I received the following feedback from Microsoft today, emphasis added.

Thanks for reporting this issue you've encountered with Visual Studio!

We've fixed up optional string parameters on attributes in our code. You'll see this fix in the version of Visual Studio after VS 2010.

Alex Turner

Program Manager

Visual Basic and C# Compiler

Sign up to request clarification or add additional context in comments.

5 Comments

Don't mind at all. I get a 404 when I follow that link though. It asked me to log in, and then said the page wasn't found.
@Mark: Strange. I just tried it out and it worked just fine. Perhaps it's a permissions thing. I'll keep you posted. I did receive an automated confirmation letter indicating it was being routed to the proper team. We'll see what happens.
It did mention permissions -- "The content that you requested cannot be found or you do not have permission to view it." So that might be it.
Huh. Interesting they gave a 404 instead of a 401, then, eh?
Well.. I just said "404" because it said "Page not found" *runs off to check the headers* It actually sent a 500.
1

Attributes already provide default-able arguments. You simply create public properties on the attribute and those can be assigned in the attribute instantiation process. It already works, it's already well-understand and it's consistent with how framework attributes work. So...why not just use that mechanism instead of trying to redundantly add default parameters to the constructor?

3 Comments

That wasn't the question :) But the reason "why" is because C# 4 added named parameters. With Attributes, it would seem that we now have two ways of defining named parameters, except that we don't, because this one raises an error; I was trying to keep the app consistent by sticking with one method. Nevertheless, I'm not saying it's a good idea to add default args, I'm just wondering why we can't.
It might be a bug, but I don't think it's critical because there is already a better way to do it. Sorry that C# isn't fully orthogonal.
For me that's definitely a bug. You can compile the code with the definition of the attribute, but whatever code that uses it can't compile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.