DisplayAttribute name with a variable, Dynamic DisplayName

前端 未结 2 476
北海茫月
北海茫月 2021-01-18 05:03

Wondering if this is possible or something with this effect.

public class MyModel
{
    public string Name { get; set; }

    [Display(Name = String.Format(\         


        
2条回答
  •  旧巷少年郎
    2021-01-18 05:32

    This isn't possible because the arguments specified for parameters of attributes must be constant values (instinctively, because there is no context in relation to anything else and not necessarily able to be resolved at compile-time (which is a requirement)). From the C# Specification (3.0) §17.2:

    An expression E is an attribute-argument-expression if all of the following statements are true:

    • The type of E is an attribute parameter type (§17.1.3).
    • At compile-time, the value of E can be resolved to one of the following:
      • A constant value.
      • A System.Type object.
      • A one-dimensional array of attribute-argument-expressions.

提交回复
热议问题