Created
January 5, 2012 16:46
-
-
Save stevenkuhn/1566061 to your computer and use it in GitHub Desktop.
ConfigurationPropertyAttribute from nexuspwn
This file contains hidden or 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
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | |
public class ConfigurationPropertyAttribute : Attribute, IComparable<ConfigurationPropertyAttribute> | |
{ | |
public ConfigurationPropertyAttribute(string displayName) | |
{ | |
DisplayName = displayName; | |
} | |
public string DisplayName { get; set; } | |
public int Order { get; set; } | |
public int CompareTo(ConfigurationPropertyAttribute other) | |
{ | |
if (other == null) | |
return 1; | |
return Order.CompareTo(other.Order); | |
} | |
} |
This file contains hidden or 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
// Example: HostProviderService.GetProperties(typeof(StaticHostProvider)) | |
// returns the list of ConfigurationProperty classes for "Location", "IPAddress", | |
// and "Port" ordered by the Order property on the ConfigurationPropertyAttribute | |
// class. I can use that list to generate the administration UI when configuring | |
// a StaticHostProvider. | |
public class HostProviderService : IHostProviderService | |
{ | |
public IEnumerable<ConfigurationProperty> GetProperties(Type type) | |
{ | |
return from prop in type.GetProperties() | |
let attr = prop.GetCustomAttributes(typeof(ConfigurationPropertyAttribute), false) | |
.SingleOrDefault() as ConfigurationPropertyAttribute | |
where attr != null | |
orderby attr.Order, attr.DisplayName | |
select new ConfigurationProperty() | |
{ | |
PropertyName = prop.Name, | |
DisplayName = attr.DisplayName | |
}; | |
} | |
} |
This file contains hidden or 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 StaticHostProvider : IHostProvider | |
{ | |
[ConfigurationProperty("IP Address", Order = 1)] | |
public string IPAddress { get; set; } | |
[ConfigurationProperty("Port", Order = 2)] | |
public string Port { get; set; } | |
[ConfigurationProperty("Location", Order = 0)] | |
public string Location { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment