roslyn: [Bug] Interpolated strings in attributes

Version Used: CS 6

Steps to Reproduce:

    class SomeClass
    {
        public const String FirstNameDef = "Pavel";

        [Required(ErrorMessage = "Field 'FirstName' is required.")]

        [Required(ErrorMessage = $"Field {nameof(FirstName)} is required.")] 
        // Error: An attribute argument must be a constant expression, 
        // typeof expression or array creation expression of an attribute parameter type

        [DefaultValue(FirstNameDef)]

        [DefaultValue($"{FirstNameDef}")]  
        // Error: An attribute argument must be a constant expression, 
        // typeof expression or array creation expression of an attribute parameter type

        public String FirstName {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public String _firstName = $"{FirstNameDef}";
    }

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 29 (18 by maintainers)

Most upvoted comments

@jaredpar Wouldn’t it make sense to have a subset of string interpolation syntax to preserve constness of the string? For example $$"Method Name: {nameof(TestMethod)}" produces a constant string. Would come in handy for performance issues discussed at #9212.

I don’t know what is the problem with translating interpolation to a bunch of string concatenation operators at the interpolation sites, in that case you may not be able to use culture dependant features like : etc. That’s why I proposed another form (double-dollar) which preserves constness of the string.