I'm working on the expando object in C#, however, I would like to know whether we have a way to assign a predefined value like 'Prop not found' when we access to a non-existing property ? My code is as follow, if we access to an unknown property in the Dictionary (cast as expandoObject) I have the Runtimebinderexception.
var dict = new ExpandoObject() as IDictionary<string, object>;
foreach (var item in xmldoc.Root.Elements("test"))
{
dict .Add(item.Attribute("key").Value, item.Element("value").Value);
}
/*Existing key, it works fine*/ var testKey = dict.TestData
/*For non existing key in the dictionary*/ var testKey = dict.NewKey; /*I have an exception here*/
So is there any way to avoid an exception and display a value like above (Prop not found) ? Thanks