Last active
October 13, 2020 19:16
-
-
Save NickStrupat/bcbc21fb9c8b43ac73ba4a1a735757ba to your computer and use it in GitHub Desktop.
Extend ExpandoObject to control assignment by hooking INotifyPropertyChanged
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 static class Extensions | |
{ | |
public static ExpandoObject WithAutoCorrect(this ExpandoObject eo) | |
{ | |
(eo as INotifyPropertyChanged).PropertyChanged += Handler; | |
return eo; | |
static void Handler(object sender, PropertyChangedEventArgs e) | |
{ | |
var dict = (IDictionary<String, Object>)sender; | |
switch (dict[e.PropertyName]) | |
{ | |
case String s: | |
dict[e.PropertyName] = (ExtendedString)s; | |
break; | |
case ExtendedString _: | |
break; | |
default: | |
throw new InvalidOperationException($"The value of the property must be of type `{typeof(String).FullName}`."); | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment