Microsoft 开发人员网络 > 论坛主页 > Windows Forms Designer > Adding Attributes to Properties At Runtime in C#...
提出问题提出问题
 

已答复Adding Attributes to Properties At Runtime in C#...

  • 2007年5月25日 13:22Rajesh_2959 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi !

     

    Currently I'm doing an MDI application which has the Toolbox, PropertyGrid and the DesignSurfaces. i'm using some third-party controls as the controls available in the Toolbox.

    This application will function similar to a VS Editor where drag and drop of controls, modifying the properites of the various control instances, adding more Tabpages, saving the Templates that was created and many more....

     

    I have an issue in using Attributes for the Properites of the different controls.
    I am using a Custom ControlDesigner to filter out certain properties and adding some properties.

    while filtering out properties,i use the BrowsableAttribute class as:

    protected override void

                    PreFilterProperties(System.Collections.IDictionary properties)
    {
             
            base.PreFilterProperties(properties);
     string[] propertiesNotToShow = {
                    "AutoCenter", "AutoFrameRate", "AutoScaleEnabled", "AutoScaleStyle", "BackGroundPicture", "BorderStyle", "CachedDrawing", "ComponentHandle"
      }  //i have some 60 odd properties
           

     foreach (string propname in propertiesNotToShow)
                {
                    prop = (PropertyDescriptor)properties[propname];
                    if (prop != null)
                    {
                        AttributeCollection runtimeAttributes = prop.Attributes;
                        // make a copy of the original attributes
                        // but make room for one extra attribute
                        Attribute[] attrs = new Attribute[runtimeAttributes.Count + 1];
                        runtimeAttributes.CopyTo(attrs, 0);
                        attrs[runtimeAttributes.Count] = new BrowsableAttribute(false); 

                      // makes this Property hidden in PropertyGrid
                        prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);
                        properties[propname] = prop;
                    }
                }
    }

    In the same approach i am not able to use the other Attributes like CategoryAttribute, DefaultValueAttribute and DesignerSerializationAttribute . Though i dont get a compile-time error, during runtime in the PropertyGrid, instead of showing the values, i get an error "Object reference not set to an instance of an object".

    Can anyone please explain why this happens with these set of attributes and for BrowsableAttribute it works fine....

答案

  • 2007年5月25日 18:55Malacki 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Typo: change prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);   to prop = TypeDescriptor.CreateProperty(control.GetType(), propname, prop.PropertyType, attrs);

    Marc.

全部回复

  • 2007年5月25日 18:55Malacki 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复
    Typo: change prop = TypeDescriptor.CreateProperty(this.GetType(), propname, prop.PropertyType, attrs);   to prop = TypeDescriptor.CreateProperty(control.GetType(), propname, prop.PropertyType, attrs);

    Marc.
  • 2007年5月28日 13:32Rajesh_2959 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    thanks a  lot Malacki !!

    It has worked :-)

     

    thanks for your time.......

     

    rajesh

  • 2009年2月9日 19:46Manfred9091 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     

    Hi rajesh,

    I've to fix the same problem as you did in 2007.

    When I try to use the same solution as you I get the error message

    "property or indexer 'System.ComponentModel.PropertyDescriptorCollection.this[string] cannot be assigned - - it is read only"

    in the line where I want to give back the attributes ( properties[propname] in your method).

    How do you get the parameter for your method PreFilterProperties and how do you connect the new attributes with the instance of your control?

    Many thanks in advanvce

    Manfred