2

I am using string interpolation for a method attribute like -

const string User = "SomeUser";
const string Admin = "Admin";
.
.
.
[Authorize(Roles = $"{User},{Admin}")]
public IHttpActionResult Get()

But Visual Studio gives an error -

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

When I try "+" for string concatenation it works

[Authorize(Roles = User + "," + Admin)]
public IHttpActionResult Get()

Even if I replace "," with ',' it gives the same error.

I wonder how the compiler is handling string interpolation?

1
  • 1
    String interpolation is just syntactic sugar for a call to string.Format, which is not a constexpr, and attribute arguments need to be constexprs. There are numerous proposals in the C# language repo regarding changing this behaviour to transform string.Format to a constexpr in certain cases like these, e.g. github.com/dotnet/csharplang/issues/384 Commented Feb 18, 2019 at 5:04

1 Answer 1

1

string interpolation is converted to string.Format which isn't a compile time constant as it requires kindly review this question for more information

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.