How To Check If A Property Exists In An Expando Object?

1.01K views
0

Is there a way to check if an Expando object contains a property before interacting with it. For example, If i were to say var test = Expando.Something and Something didn’t exist, it will throw an exception. How do i check to see if “Something” exists in the expando object before I query it?

Changed status to publish
0

Expando objects are just an abstraction. The class inherits from IDictionary<string, object>. This class supports the ContainsKey method and this will do what you need it to do. So if you cast the expando object to a dictionary first, you can use the contains key method to check if a key exists.

Use the following if statement to check if a property exists in an expando object.

Changed status to publish