How compiler handles string interpolation

天涯浪子 提交于 2019-12-20 04:30:50

问题


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:


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



来源:https://stackoverflow.com/questions/54740725/how-compiler-handles-string-interpolation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!