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

0
2593
Asked By William Rossbach On

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?

1 Answer

Answered By Dan On

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.

if(((IDictionary<String, object>)expandoObject).ContainsKey("SomeMember") 

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.