Here is a DynamicDataObject
class derived from 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 I am consuming DynamicDataObject
like below.
public MainWindow()
{
InitializeComponent();
dynamic person = new DynamicDataObject();
person.FirstName = "Vimal";
person.LastName = "Adams";
person.Address = null;
}
I can see all members of person
and it's values in the _dataDictionary
but at the same time the debugger view excludes members having null
value. So that person.Address
member is not visible in the dynamic view collection.(please see the below screenshot). Can anyone please help me understanding why DynamicObject
behaves differently in this scenario?