In javascript you can detect if a property is defined by using the undefined keyword:
if( typeof data.myProperty == "undefined" ) ...
How would you do this in C# using the dynamic keyword with an ExpandoObject
and without throwing an exception?
I wanted to create an extension method so I could do something like:
... but you can't create extensions on
ExpandoObject
according to the C# 5 documentation (more info here).So I ended up creating a class helper:
To use it:
Try this one
This extension method checks for the existence of a property and then returns the value or null. This is useful if you do not want your applications to throw unnecessary exceptions, at least ones you can help.
If can be used as such :
or if your local variable is 'dynamic' you will have to cast it to ExpandoObject first.