Skip to content

Instantly share code, notes, and snippets.

@stevenkuhn
Created January 5, 2012 16:46
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 stevenkuhn/1566061 to your computer and use it in GitHub Desktop.
Save stevenkuhn/1566061 to your computer and use it in GitHub Desktop.
ConfigurationPropertyAttribute from nexuspwn
[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);
}
}
// 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
};
}
}
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