Search code examples
c#asp.net-mvcmodels

Use a variable in an attribute constructor


Is it possible to use a variable in an attribute constructor, something like this below:

[Foo(bar)]
public string Baz { get; set; }

Solution

  • The question is valid, but it contains references to irrelevant context. It should really be written like so:


    Is it possible to use a variable in an attribute constructor, something like this below:

    [Foo(bar)]
    public string Baz { get; set; }
    

    The answer, as Stephen Muecke says, is no.

    attributes add metadata and are evaluated a compile-time so therefore must be constants