I have a dynamic object that looks as follows:
this.ChartDetails.Chart
‘Chart’ is dynamic. I want to see if a dynamic property exists on Chart named LeftYAxis. What is the best way to do this on dynamic objects?
I don’t think this is a duplicate of How to detect if a property exists on an ExpandoObject? because it doesn’t discuss the best method to do this for dynamic objects.
This is what happens at runtime in the first place. (When you access a property the ‘dynamic’ piece of things only happens when a first-chance exception gets handled by the object’s override of
DynamicObject‘sTryGetMemberandTrySetMemberSome objects (like
ExpandoObject) are actually dictionaries under the hood and you can check them directly as follows:Basically: without knowing what actual type
ChartDetails.Chartis (if it’s anExpandoObjecta plain ol’ subclass ofobjector a subclass ofDynamicObject) there’s no way besides the try/catch above. If you wrote the code forChartDetailsandChartor have access to the source code you can determine what methods exist for the object and use those to check.