Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I add a ExpandoObject to an already existing ExpandoObject?

Here is my code :

 dynamic App = new ExpandoObject();
 //App names are SAP, CRM and ERP 

        //App names - adding static
        //App.SAP = new ExpandoObject();
        //App.CRM = new ExpandoObject();
        //App.ERP = new ExpandoObject();

The last 4 lines, I am adding an expando object static as i know the app names previously. But I want to do this dynamically.

I can do for the properties dynamically :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        AddProperty(App.SAP, "Name", "sap name");
        AddProperty(App.SAP, "UserID", "sap userid");
        AddProperty(App.SAP, "EmailID", "userid@sap.com");
        AddProperty(App.SAP, "GroupOf", "group1, group2, group3");


 public static void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
    {
        // ExpandoObject supports IDictionary so we can extend it like this
        var expandoDict = expando as IDictionary<string, object>;
        if (expandoDict.ContainsKey(propertyName))
            expandoDict[propertyName] = propertyValue;
        else
            expandoDict.Add(propertyName, propertyValue);
    }

but I need to add this object App.SAP to this App which is also an expandoobject, so I can add App.CRM or App.ERP dynamically later on.

>Solution :

You will get

ExpandoObject doesn’t have a definition for SAP

with this approach:

AddProperty(App.SAP, "Name", "sap name");

as you didn’t declare the SAP in APP ExpandoObject.


Instead, you can try to modify the AddProperty method by providing the parentName and deal with the nested object.

AddProperty(App, "SAP", "Name", "sap name");
AddProperty(App, "SAP", "UserID", "sap userid");
AddProperty(App, "SAP", "EmailID", "userid@sap.com");
AddProperty(App, "SAP", "GroupOf", "group1, group2, group3");
public static void AddProperty(ExpandoObject expando, string parentName, string propertyName, object propertyValue)
{
    // ExpandoObject supports IDictionary so we can extend it like this
    var expandoDict = expando as IDictionary<string, object>;
    if (expandoDict.ContainsKey(parentName))
    {
        ((IDictionary<string, object>)expandoDict[parentName])[propertyName] = propertyValue;
    }
    else
    {
        dynamic child = new Dictionary<string, object>
        {
            { propertyName,  propertyValue }
        };
            
        expandoDict.Add(parentName, child);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading