none
Get and set custom attributes on dynamic object

    Question

  • Hi guys,

    I´m trying something different here.

    I need to create a dynamic object with dynamic properties too, and I found this way to help me:

     class ObjectWithProperties : ICustomTypeDescriptor
        {
            class MyDescriptor : PropertyDescriptor
            {
                public MyDescriptor(string name)
                    : base(name, new[] { new Atributos.AtributosdasPropriedades()})
                {

                }


    Dictionary<string, object> properties = new Dictionary<string, object>();
    public object this[string name]
    {
       get
       {
           if (properties.ContainsKey(name))
           {
               return properties[name];
           }
           return null;
       }
       set
       {
           properties[name] = value;
       }
    }

    As you can see, in my case i need to give some attributes to that property.

    Well, that is my problem!

    When I create my object, I don´t know how to set this attributes, can be on runtime or not!

    I create the object like this:

    ObjectWithProperties obj1 = new ObjectWithProperties();
    obj1["property1"] = 100;
    obj1["property2"] = 200;
    obj1["property3"] = 300;

    I need to set, and in other time get that attributes!

    It is possible to create custom attributes on a object created like my example?

     

    Monday, January 31, 2011 10:20 PM

Answers

  • If you don't have it, it means you have to write it. If you're using the ObjectWithProperties from http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/69cefc96-351e-41c8-a2c6-e6e217459366

    you could add something like this:

      Dictionary<string, List<Attribute>> attributes = new Dictionary<string, List<Attribute>>();
      public void SetAttribute(string property, Attribute attribute)
      {
        if (attributes.ContainsKey(property))
          attributes[property].Add(attribute);
        else
          attributes[property] = new List<Attribute> { attribute };
      }
    

    Add a constructor in the PropertyDescriptor:

    public MyDescriptor(string name, Attribute[] attributes) : base(name, attributes) { }
    

    And change the GetProperties method to call this new constructor:

    return new PropertyDescriptorCollection(properties.Keys.Select(
      key => this.attributes.ContainsKey(key)
        ? new MyDescriptor(key, this.attributes[key].ToArray())
        : new MyDescriptor(key)).ToArray());
    
    • Marked as answer by PHS Brasil Tuesday, February 01, 2011 3:37 PM
    Tuesday, February 01, 2011 1:00 PM

All replies

  • You'd add an attribute like this:

    obj1.SetAttribute("property1", new SomeAttribute(someValue));
    

    For this, you need to maintain a collection of attributes for each key in the properties dictionary. Pass that collection of attributes to the constructor of the PropertyDescriptor.

    Monday, January 31, 2011 11:12 PM
  • Hi Louis.fr,

    I don´t have this SetAttribute method on my collection, can you please give me some example? some article, website or anything?

    Thank you very much!

    Monday, January 31, 2011 11:40 PM
  • If you don't have it, it means you have to write it. If you're using the ObjectWithProperties from http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/69cefc96-351e-41c8-a2c6-e6e217459366

    you could add something like this:

      Dictionary<string, List<Attribute>> attributes = new Dictionary<string, List<Attribute>>();
      public void SetAttribute(string property, Attribute attribute)
      {
        if (attributes.ContainsKey(property))
          attributes[property].Add(attribute);
        else
          attributes[property] = new List<Attribute> { attribute };
      }
    

    Add a constructor in the PropertyDescriptor:

    public MyDescriptor(string name, Attribute[] attributes) : base(name, attributes) { }
    

    And change the GetProperties method to call this new constructor:

    return new PropertyDescriptorCollection(properties.Keys.Select(
      key => this.attributes.ContainsKey(key)
        ? new MyDescriptor(key, this.attributes[key].ToArray())
        : new MyDescriptor(key)).ToArray());
    
    • Marked as answer by PHS Brasil Tuesday, February 01, 2011 3:37 PM
    Tuesday, February 01, 2011 1:00 PM
  • Hi All,

    Thanks for all your participate and experience share.

    Have a good day.


    David Peng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, February 03, 2011 6:34 AM
    Moderator