c# - DynamicObject behaves differently for null values -
here dynamicdataobject
class derived dynamicobject
public class dynamicdataobject : dynamicobject { private readonly dictionary<string, object> _datadictionary = new dictionary<string, object>(); public override bool trygetmember(getmemberbinder binder, out object result) { return _datadictionary.trygetvalue(binder.name, out result); } public override bool trysetmember(setmemberbinder binder, object value) { if (!_datadictionary.containskey(binder.name)) { _datadictionary.add(binder.name, value); return true; } return false; } public override ienumerable<string> getdynamicmembernames() { return _datadictionary.keys; } }
and consuming dynamicdataobject
below.
public mainwindow() { initializecomponent(); dynamic person = new dynamicdataobject(); person.firstname = "vimal"; person.lastname = "adams"; person.address = null; }
i can see members of person
, it's values in _datadictionary
@ same time debugger view excludes members having null
value. person.address
member not visible in dynamic view collection.(please see below screenshot). can please me understanding why dynamicobject
behaves differently in scenario?
i think optimization. there no use in keeping reference default value of type. returns default(t)
when try access it.
it behaves dictionary:
string property = "address"; object val; if (!dic.trygetvalue(property, out val)) { return default(t); } else { return (t)val; // simplification }
what point here keep address
in dictionary? none. why removed.
Comments
Post a Comment