DisplayAttribute name with variable, Dynamic DisplayName

I wonder if this is possible or something with this effect.

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

    [Display(Name = String.Format("This is [0] phone number", Name)]
    public string PhoneNumber { get; set; }
}

I am talking about DisplayName with a variable in it that is not static, and possibly based on models of other properties. Is this possible anyway?

+5
source share
2 answers

This is not possible because the arguments specified for attribute parameters must be constant values ​​(instinctively, because there is no context in relation to anything else and may not necessarily be resolved at compile time (which is a requirement)). From the C # specification (3.0) Β§17.2:

An expression Eis an expression of an argument attribute if all of the following statements are true:

  • E - (Β§17.1.3).
  • E :
    • .
    • System.Type.
    • --.
+3

, , , DisplayNameAttribute, DescriptionAttribute CategoryAttribute, (resx, database, whatever). , .

, , : !

, , PropertyGrid, DataGridView .., : ICustomTypeDescriptor, TypeDescriptionProvider, , , . / . , TypeConverter , ICustomTypeDescriptor/TypeDescriptionProvider, PropertyDescriptor.

; , ! , .

+3

All Articles