views:

1099

answers:

2

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.

+1  A: 

The static property is accessed via CategoryAttribute.Appearance. But the attribute system does not allow you to invoke code in an attribute declaration and I guess that is why it wont compile for you. You will probably have to settle for [Category("Appearance")].

Peter Lillevold
+1  A: 

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.

BFree
hm, that's just plain annoying... hehe. what's the point in a constant defined attribute when it is only usable in one end. Oh well. Thanks for the info :)
Svish