Skip to content

Instantly share code, notes, and snippets.

@angelovstanton
Created August 23, 2015 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelovstanton/98184790d4fea6444f37 to your computer and use it in GitHub Desktop.
Save angelovstanton/98184790d4fea6444f37 to your computer and use it in GitHub Desktop.
public class DefaultValueAttributeTest
{
public DefaultValueAttributeTest()
{
// Use the DefaultValue propety of each property to actually set it, via reflection.
foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(this))
{
DefaultValueAttribute attr = (DefaultValueAttribute)prop.Attributes[typeof(DefaultValueAttribute)];
if (attr != null)
{
prop.SetValue(this, attr.Value);
}
}
}
[DefaultValue(25)]
public int Age { get; set; }
[DefaultValue("Anton")]
public string FirstName { get; set; }
[DefaultValue("Angelov")]
public string LastName { get; set; }
public override string ToString()
{
return string.Format("{0} {1} is {2}.", this.FirstName, this.LastName, this.Age);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment