using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
public static void Main()
var fb = new FizzyBuzzy();
fb.PropertyChanged += (s, e) => {
var _fb = s as FizzyBuzzy;
if(e.PropertyName == "Value")
Console.WriteLine("{0:0000} {1}{2}", _fb.Value, _fb.Fizz(),_fb.Buzz());
for(int i = 1; i <= 100; i++)
public class FizzyBuzzy : PropertyNotification
public int Value { get {return Get();} set {Set(value);} }
return (Value % 3 == 0) ? "Fizz" : "";
return (Value % 5 == 0) ? "Buzz" : "";
public abstract class PropertyNotification : INotifyPropertyChanged
private Dictionary<string, object> _p = new Dictionary<string, object>();
private Dictionary<string, Type> _t = new Dictionary<string, Type>();
private static Dictionary<Type, object> _d = new Dictionary<Type, object>();
private static T GetDefault<T>() { return default(T); }
protected dynamic Get([CallerMemberName] string name = null)
if(!_t.TryGetValue(name, out t))
t = GetType().GetProperty(name).PropertyType;
if(!_d.TryGetValue(t, out d))
Func<object> f = GetDefault<object>;
d = f.Method.GetGenericMethodDefinition().MakeGenericMethod(t).Invoke(null, null);
if (_p.TryGetValue(name, out v))
return v == null ? d : v;
protected void Set<T>(T value, [CallerMemberName] string name = null)
if (Equals(value, Get(name)))
_t[name] = value.GetType();
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
PropertyChangedEventHandler handler = PropertyChanged;
handler(this, new PropertyChangedEventArgs(propertyName));