Skip to content

Instantly share code, notes, and snippets.

@cobysy
Created June 9, 2015 09:08
Show Gist options
  • Save cobysy/1cde1ed95db64a920491 to your computer and use it in GitHub Desktop.
Save cobysy/1cde1ed95db64a920491 to your computer and use it in GitHub Desktop.
/// Ruthlessly inspired by from http://kozmic.net/2014/03/22/strongly-typed-app-settings-with-castle-dictionaryadapter/
public class AppSettingRequiredAttribute : Attribute
{
}
public class AppSettingWrapperAttribute : DictionaryBehaviorAttribute, IDictionaryPropertyGetter, IPropertyDescriptorInitializer
{
object IDictionaryPropertyGetter.GetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, object storedValue, PropertyDescriptor property, bool ifExists)
{
if (storedValue != null) return storedValue;
if (property.Property.HasAttribute<AppSettingRequiredAttribute>())
throw new InvalidOperationException(string.Format("Required app setting '{0}' not found!", key));
return null;
}
// enable this to validate property values that were read and set
void IPropertyDescriptorInitializer.Initialize(PropertyDescriptor propertyDescriptor, object[] behaviors)
{
propertyDescriptor.Fetch = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment