I have a property, (example shown below).
[DefaultValue(false)]
public bool MyProperty {
get {
return myVal;
}
set {
myVal=value;
}
}
The situation I’m using this is to make sure it shows up as bold in the a PropertyGrid if the default value is not set.
I find it incredibly annoying that in my constructor, I have to set the initial value of my property, and hope that they match up.
Is it possible to have my constructor “discover” the default value of a given property, and set it accordingly? Something like:
myctor()
{
myVal = GetDefaultValueProperty<bool>("MyProperty");
}
You can use the following code to get the metadata you’re after.
If you want to do something really cool, you can do this with a Lambda expression:
Usage is then: