Skip to content

Instantly share code, notes, and snippets.

@NickStrupat
Last active October 13, 2020 19:16
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 NickStrupat/bcbc21fb9c8b43ac73ba4a1a735757ba to your computer and use it in GitHub Desktop.
Save NickStrupat/bcbc21fb9c8b43ac73ba4a1a735757ba to your computer and use it in GitHub Desktop.
Extend ExpandoObject to control assignment by hooking INotifyPropertyChanged
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