How do i set the default value of an enumerated property?
e.g.:
public enum SearchBoxMode { Instant, Regular };
[DefaultValue(SearchBoxMode.Instant)]
public SearchBoxMode Mode { get { return mode; } set { mode = value; } }
[DefaultValue((int)SearchBoxMode.Instant)]
public SearchBoxMode Mode { get { return mode; } set { mode = value; } }
[DefaultValue(SearchBoxMode.GetType(), "Instant")]
public SearchBoxMode Mode { get { return mode; } set { mode = value; } }
[DefaultValue(SearchBoxMode.GetType(), "SearchBoxMode.Instant")]
public SearchBoxMode Mode { get { return mode; } set { mode = value; } }
Unrelated question: How do i get the Type of an enumeration? e.g.
Type type = DialogResult.GetType();
does not work.
The default value of an enum is 0 of the underyling type, even if 0 isn’t defined for that enum. Anything else must be done manually, for example:
Using
[DefaultValue(...)]only impacts things like serialization andPropertyGrid– it doesn’t actually make the property default to that value. The correct syntax is as per your first example:Another approach is a constructor:
re the second question;
typeof(DialogResult)