Created
June 9, 2015 09:08
-
-
Save cobysy/1cde1ed95db64a920491 to your computer and use it in GitHub Desktop.
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
/// 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