public class DynamicClass : DynamicObject { private Dictionary<string, KeyValuePair<Type, object>> _fields; public DynamicClass(List<Field> fields) { _fields = new Dictionary<string, KeyValuePair<Type, object>>(); fields.ForEach(x => _fields.Add(x.FieldName, new KeyValuePair<Type, object>(x.FieldType, null))); } public override bool TrySetMember(SetMemberBinder binder, object value) { if (_fields.ContainsKey(binder.Name)) { var type = _fields[binder.Name].Key; if (value.GetType() == type) { _fields[binder.Name] = new KeyValuePair<Type, object>(type, value); return true; } else throw new Exception("Value " + value + " is not of type " + type.Name); } return false; } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = _fields[binder.Name].Value; return true; } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter