none
Creating properties dynamically and to add attributes to the them to display in the property grid RRS feed

  • Question

  • Hi all,

    I need to create properties dynamically and display them in the property grid. Also, I need to add attributes to this newly created run time property. For the second part, (ie) to add attributes I have already achieved them by implementing ICustomTypeDescriptor.

    Now, for the first part, I tried to add properties to the ExpandoObject like below and also implemented ICustomTypeDescriptor (to tell TypeDescriptor to pull the properties while working with ExpandoObject)

    dynamic dynObj = new ExpandoObject();

    dynObj.Add("Key1", "hello");

    dynObj.Add("Key2", 45);

    TypeDescriptor.AddProvider(new ExpandoObjectTypeDescriptionProvider(), dynObj);

    But, I'm stuck not knowing how to  make this dynObj avialble as part of my class, (i.e) to 'this ' object, so as to iterate over all the properties in 'this' to add new attributes to it.


    Below[part of the code snippet], is how I'm adding new attributes to the existing property. While it works for all static properties, I don't know how to iterate through for dynamic object...



    PropertyOverridingTypeDescriptor ctd = new PropertyOverridingTypeDescriptor(TypeDescriptor.GetProvider(this).GetTypeDescriptor(this));

    foreach(PropertyDescriptor pd in TypeDescriptor.GetProperties(this))
                {

                        PropertyDescriptor pd2 = TypeDescriptor.CreateProperty(pd.GetType(), pd, new Attribute[2] { new OutputPropertyAttribute(), new BrowsableAttribute(false)}
                            );
                        ctd.OverrideProperty(pd2);                    
                    }
                TypeDescriptor.AddProvider(new TypeDescriptorOverridingProvider(ctd), this);
            }

    Please, help me out here

    • Edited by jamie15 Thursday, March 3, 2016 4:27 AM
    Thursday, March 3, 2016 4:25 AM

Answers

  • Hi jbv2015,

    Firstly, I've tested on your code.

    dynamic dynObj = new ExpandoObject();
    
    dynObj.Add("Key1", "hello");
    
    dynObj.Add("Key2", 45);
    

    You'll meet a error: 'System.Dynamic.ExpandoObject' does not contain a definition for 'Add'.

    If you want to create properties dynamically, you could do as following

    dynamic employee = new ExpandoObject();
    employee.Name = "John Smith";
    employee.Age = 33;
     

    Please also refer to https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx for more details.

    >>But, I'm stuck not knowing how to  make this dynObj avialble as part of my class, (i.e) to 'this ' object, so as to iterate over all the properties in 'this' to add new attributes to it.

    Secondly, dynObj and 'this' are different instance of a class, you cannot do that as a same object.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Kristin Xie Monday, March 14, 2016 5:39 AM
    • Marked as answer by DotNet WangModerator Tuesday, March 15, 2016 1:25 AM
    • Unmarked as answer by jamie15 Tuesday, March 15, 2016 6:44 PM
    • Marked as answer by jamie15 Tuesday, March 15, 2016 6:45 PM
    Friday, March 4, 2016 3:18 AM
  • Hi Kristen,
    Thanks for your reply.
    The main problem I had was not being able to add the dynamically created object to the current class.
    I was not able to accomplish them using ExpandoObject. I found a different route to get them working.
    I figured out how we add the properties dynamically to the collection editor(in C#). In my case, I had a custom collection editor to which I add properties dynamically. So trying to use the same functionality upon certain conditions enabled me to achieve adding properties via code but only when required.
    I hope this can be helpful for someone having the same issue like I had.

    Thanks!

    • Marked as answer by jamie15 Tuesday, March 15, 2016 6:44 PM
    Tuesday, March 15, 2016 6:43 PM

All replies

  • Hi jbv2015,

    Firstly, I've tested on your code.

    dynamic dynObj = new ExpandoObject();
    
    dynObj.Add("Key1", "hello");
    
    dynObj.Add("Key2", 45);
    

    You'll meet a error: 'System.Dynamic.ExpandoObject' does not contain a definition for 'Add'.

    If you want to create properties dynamically, you could do as following

    dynamic employee = new ExpandoObject();
    employee.Name = "John Smith";
    employee.Age = 33;
     

    Please also refer to https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx for more details.

    >>But, I'm stuck not knowing how to  make this dynObj avialble as part of my class, (i.e) to 'this ' object, so as to iterate over all the properties in 'this' to add new attributes to it.

    Secondly, dynObj and 'this' are different instance of a class, you cannot do that as a same object.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Kristin Xie Monday, March 14, 2016 5:39 AM
    • Marked as answer by DotNet WangModerator Tuesday, March 15, 2016 1:25 AM
    • Unmarked as answer by jamie15 Tuesday, March 15, 2016 6:44 PM
    • Marked as answer by jamie15 Tuesday, March 15, 2016 6:45 PM
    Friday, March 4, 2016 3:18 AM
  • Hi jbv2015,

    How about the problem now? Do you have any updates about my suggestion?

    Please remember to close this thread by marking helpful reply as an answer. If you have any other questions, please feel free to reopen a new thread in this forum. Thanks for your understanding.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 14, 2016 5:39 AM
  • Hi Kristen,
    Thanks for your reply.
    The main problem I had was not being able to add the dynamically created object to the current class.
    I was not able to accomplish them using ExpandoObject. I found a different route to get them working.
    I figured out how we add the properties dynamically to the collection editor(in C#). In my case, I had a custom collection editor to which I add properties dynamically. So trying to use the same functionality upon certain conditions enabled me to achieve adding properties via code but only when required.
    I hope this can be helpful for someone having the same issue like I had.

    Thanks!

    • Marked as answer by jamie15 Tuesday, March 15, 2016 6:44 PM
    Tuesday, March 15, 2016 6:43 PM