Created
August 23, 2015 12:18
-
-
Save angelovstanton/98184790d4fea6444f37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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