4

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.

Svish
  • 152,914
  • 173
  • 462
  • 620

3 Answers3

4

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
  • 102,548
  • 21
  • 159
  • 201
  • 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 Mar 05 '09 at 21:19
  • May be you could create your own Categories Class. Take a look [here](http://weblogs.asp.net/sfeldman/archive/2009/05/07/categoryattribute.aspx) – Ivan Ferrer Villa Nov 09 '11 at 11:34
2

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
  • 33,668
  • 7
  • 97
  • 131
0

Starting from C# 6 there is finally a better alternative: [Category(nameof(CategoryAttribute.Appearance))].

glopes
  • 4,038
  • 3
  • 26
  • 29