5

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.

Community
  • 1
  • 1
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • if this.ChartDetails.Chart.LeftYAxis != null – John Gathogo Jun 05 '12 at 14:56
  • Is this is *your* implementation of DynamicObject ? – Tigran Jun 05 '12 at 14:56
  • 3
    possible duplicate of [how to detect if a property exists on a dynamic object in C#?](http://stackoverflow.com/questions/2839598/how-to-detect-if-a-property-exists-on-a-dynamic-object-in-c) – jason Jun 05 '12 at 14:56
  • @JohnGathogo - That does not work. An exception will get thrown because when the object does not exist. – Randy Minder Jun 05 '12 at 14:59
  • I tried the method posted by @JohnGathogo and it worked fine. Under what circumstances does it cause an exception to be thrown? – joelmdev Mar 03 '14 at 20:52
  • possible duplicate of [dynamic, How to test if a property is available](http://stackoverflow.com/questions/2998954/dynamic-how-to-test-if-a-property-is-available) – nawfal Jul 19 '14 at 21:26

4 Answers4

7

For a variety of reasons it's best to avoid try/catch blocks for control flow. Therefore, while Christopher's method attains the desired result, I find this preferable:

this.ChartDetails.Chart.GetType().GetProperty("LeftYAxis") != null;
joelmdev
  • 11,083
  • 10
  • 65
  • 89
6
bool isDefined = false;
object axis = null;
try
{
    axis = this.ChartDetails.Chart.LeftYAxis;
    isDefined = true;
}
catch(RuntimeBinderException)
{ }

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's TryGetMember and TrySetMember

Some objects (like ExpandoObject) are actually dictionaries under the hood and you can check them directly as follows:

bool isDefined = ((IDictionary<string, object>)this.ChartDetails.Chart)
    .ContainsKey("LeftYAxis");

Basically: without knowing what actual type ChartDetails.Chart is (if it's an ExpandoObject a plain ol' subclass of object or a subclass of DynamicObject) there's no way besides the try/catch above. If you wrote the code for ChartDetails and Chart or have access to the source code you can determine what methods exist for the object and use those to check.

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111
1

This Works if the dynamic object is a json/open-standard format.

I made two different method helpers one for open-standard format and one for "standard object".

    // Checks if object typed json or xml has a specific property
    public static bool IsPropertyExistsOpenStandardFormat(dynamic dynamicObject, string propertyName)
    {
        try
        {
            var x = dynamicObject[propertyName];
            if (x != null)
                return true;
        }
        catch 
        {
            return false;
        }

    }

    // Checks if standard object has a specific property
    public static bool IsPropertyExistsStandard(dynamic dynamicObject, string propertyName)
    {
        return dynamicObject.GetType().GetProperty(propertyName) != null;
    }
AKO
  • 9
  • 3
-1

This one is working -:

public static bool IsPropertyExists(dynamic dynamicObj, string property)
       {
           try
           {
               var value=dynamicObj[property].Value;
               return true;
           }
           catch (RuntimeBinderException)
           {

               return false;
           }

       }
user3359453
  • 349
  • 3
  • 16