1

I am attempting to store values for a displayName attribute from a setting held in the app.config file.

[System.ComponentModel.DisplayName(Properties.Settings.Default.field2Name)]

This does not work because it must be a constant value, which Properties.Settings.Default clearly is not. Is there any simple way to get around this?

Tim
  • 1,334
  • 2
  • 15
  • 29

1 Answers1

6

Since the DisplayName property is virtual, you could do something like that:

public class DisplayNameSettingsKeyAttribute : DisplayNameAttribute
{
    private readonly string _settingsKey;

    public DisplayNameSettingsKeyAttribute(string settingsKey)
    {
        _settingsKey = settingsKey;
    }

    public string SettingsKey
    {
        get { return _settingsKey; }
    }

    public override string DisplayName
    {
        get { return (string)Properties.Settings.Default[_settingsKey]; }
    }
}

And use it like that:

[DisplayNameSettingsKey("field2Name")]
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758