Microsoft Developer Network > 포럼 홈 > Windows Forms Designer > Adding Attributes to Properties At Runtime in C#...
질문하기질문하기
 

답변됨Adding Attributes to Properties At Runtime in C#...

  • 2007년 5월 25일 금요일 오후 1: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일 금요일 오후 6: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일 금요일 오후 6: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일 월요일 오후 1:32Rajesh_2959 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

    thanks a  lot Malacki !!

    It has worked :-)

     

    thanks for your time.......

     

    rajesh

  • 2009년 2월 9일 월요일 오후 7: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