I have read a bit about Design-Time Attributes for Components. There I found an attribute called CategoryAttribute. On that page it says that
我已经阅读了一些关于组件的设计时属性。在那里,我找到了一个名为CategoryAttribute的属性。在那个页面上它说
The CategoryAttribute class defines the following common categories:
CategoryAttribute类定义以下常见类别:
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?
然后列出一些常见的类别。其中一个是例如外观。我想,太棒了!然后我可以使用[Category.Appearance]而不是[Category(“Appearance”)]!但显然我不能?试图写它,但Intellisense不会接受它,它不会编译。我在这里错过了什么吗?是不是这些属性不适合?如果没有,它们是为了什么?如果是,我该如何使用它们?
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.
是的,我确实有正确的使用来访问CategoryAttribute,因为[Category(“Whatever”)]确实有效。我只是想知道我如何使用这些定义的常见类别。
2 个解决方案
#1
As you can see on MSDN it's only a getter property, not a setter.
正如你在MSDN上看到的那样,它只是一个getter属性,而不是一个setter。
public static CategoryAttribute Appearance { get; }
In fact, here's what the code looks like using Reflector:
实际上,这是使用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.
基本上,当使用反射来查看类时,您可以轻松地检查它属于哪个类别而无需进行字符串比较。但是你不能以你想要的方式使用它。
#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")].
可以通过CategoryAttribute.Appearance访问static属性。但属性系统不允许您在属性声明中调用代码,我猜这就是为什么它不会为您编译。你可能不得不满足于[类别(“外观”)]。
#1
As you can see on MSDN it's only a getter property, not a setter.
正如你在MSDN上看到的那样,它只是一个getter属性,而不是一个setter。
public static CategoryAttribute Appearance { get; }
In fact, here's what the code looks like using Reflector:
实际上,这是使用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.
基本上,当使用反射来查看类时,您可以轻松地检查它属于哪个类别而无需进行字符串比较。但是你不能以你想要的方式使用它。
#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")].
可以通过CategoryAttribute.Appearance访问static属性。但属性系统不允许您在属性声明中调用代码,我猜这就是为什么它不会为您编译。你可能不得不满足于[类别(“外观”)]。