Dynamic Object - How to tell if a property is defined?

15,036

Solution 1

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.

Solution 2

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;

Solution 3

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;
    }
Share:
15,036
Randy Minder
Author by

Randy Minder

Azure Data Architect / Business Intelligence / Tabular Modeling / Power BI Significant experience with data architecture (database architecture, data warehouses, data marts), SSIS and Azure Data Factory, Business Intelligence with Power BI. Check out my new course on Udemy titled "The DAX Workshop Part 1". The best way to learn DAX is by working through real-world scenarios. The course is filled with exercises (45) to help you learn how to use DAX. Our careers and hobbies are fun and important. But each of us has a soul which will live forever, after our bodies die. Do you know where you'll spend eternity? Jesus Christ said there is only one way to heaven, and it is through Him. You won't get a second chance after you die.

Updated on June 14, 2022

Comments

  • Randy Minder
    Randy Minder about 2 years

    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.