Search code examples
c#user-controlsattributeswindows-forms-designer

C#: How to use CategoryAttribute.Appearance Property


I have read a bit about Design-Time Attributes for Components. There I found an attribute called CategoryAttribute. On that page it says that

The CategoryAttribute class defines the following common categories:

And then lists up a number of common categories. One of them are for example Appearance. I thought, brilliant! Then I can use [Category.Appearance] instead of [Category("Appearance")]! But apparently I couldn't? Tried to write it, but Intellisense wouldn't pick it up and it wouldn't compile. Am I missing something here? Was it maybe not this those properties were for? If not, what are they for? If they are, how do I use them?

And yes, I do have the correct using to have access to the CategoryAttribute, cause [Category("Whatever")] do work. I'm just wondering how I use those defined common categories.


Solution

  • As you can see on MSDN it's only a getter property, not a setter.

    public static CategoryAttribute Appearance { get; }
    

    In fact, here's what the code looks like using Reflector:

     public static CategoryAttribute Appearance
        {
            get
            {
                if (appearance == null)
                {
                    appearance = new CategoryAttribute("Appearance");
                }
                return appearance;
            }
        }
    

    So it doesn't do a heck of a lot.

    The only use I can see for it, is something like this:

                foreach (CategoryAttribute attrib in prop.GetCustomAttributes(typeof(CategoryAttribute), false))
                {
                    bool result = attrib.Equals(CategoryAttribute.Appearance);
                }
    

    Basically, when using reflection to look at the class, you can easily check which category this belongs to without having to do a String comparison. But you can't use it in the manner you're trying to unfortunately.