c# - Dynamic Object - How to tell if a property is defined? -
i have dynamic object looks follows:
this.chartdetails.chart 'chart' dynamic. want see if dynamic property exists on chart named leftyaxis. best way on dynamic objects?
i don't think duplicate of how detect if property exists on expandoobject? because doesn't discuss best method dynamic objects.
bool isdefined = false; object axis = null; try { axis = this.chartdetails.chart.leftyaxis; isdefined = true; } catch(runtimebinderexception) { } this happens @ runtime in first place. (when access property 'dynamic' piece of things happens when first-chance exception gets handled object's override of dynamicobject's trygetmember , trysetmember
some objects (like expandoobject) dictionaries under hood , can check them directly follows:
bool isdefined = ((idictionary<string, object>)this.chartdetails.chart) .containskey("leftyaxis"); basically: without knowing actual type chartdetails.chart (if it's expandoobject plain ol' subclass of object or subclass of dynamicobject) there's no way besides try/catch above. if wrote code chartdetails , chart or have access source code can determine methods exist object , use check.
Comments
Post a Comment