简体   繁体   English

方括号[]在下面的代码中是什么意思?

[英]What does square bracket [] mean in the below code?

I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane. 我从http://msdn.microsoft.com/en-us/library/dd584174(office.11​​).aspx获取以下代码,用于在webpart工具窗格中添加自定义属性。 What does square bracket ( [] ) mean in the below code? 方括号( [] )在下面的代码中的含义是什么?

[Category("Custom Properties")]
        [WebPartStorage(Storage.Personal)]
        [FriendlyNameAttribute("Custom Color")]
        [Description("Select a color from the dropdown list.")]
        [Browsable(true)]
        [XmlElement(typeof(System.Drawing.KnownColor))]
        public System.Drawing.KnownColor MyColor
        {
            get
            {
                return _myColor;
            }
            set
            {
                _myColor = value;
            }
        }

They're called attributes. 他们被称为属性。

Here's a quick example of how they can be used: http://www.codeproject.com/KB/cs/attributes.aspx 以下是一个如何使用它们的简单示例: http//www.codeproject.com/KB/cs/attributes.aspx

As @Spencer Ruport said, they're attributes. 正如@Spencer Ruport所说,他们是属性。 They're used within .NET for declarative programming . 它们在.NET中用于声明性编程

You can find information on each of these attributes at MSDN. 您可以在MSDN上找到有关这些属性的信息。 However, you should know that the name of the attribute can be shortened. 但是,您应该知道可以缩短属性的名称。 In your case, for example, Category is the short form of the class name CategoryAttribute and XmlElement is the short form of the class name XmlElementAttribute . 在您的情况下,例如, Category是类名CategoryAttribute的缩写形式, XmlElement是类名XmlElementAttribute的缩写形式。 When declaring attributes, the Attribute portion of the class name can be left out. 声明属性时,可以省略类名的Attribute部分。

I've used most of these attributes in conjunction with the PropertyGrid control (see here for an example), although in your case, they are used for a Web Part property pane. 我已将大多数这些属性与PropertyGrid控件结合使用(请参阅此处的示例),尽管在您的情况下,它们用于Web部件属性窗格。 The purpose is still the same. 目的仍然是一样的。 The attributes are used by the control to know how to display the property to the user. 控件使用这些属性来了解如何向用户显示属性。 By using a combination of the various attributes that the control understands, it is possible to declaratively dictate this behavior. 通过使用控件理解的各种属性的组合,可以声明性地指示此行为。

I hope that helps a little bit, but Spencer is correct, you'll learn a lot more reading about attributes via Google than I can explain here. 我希望这有点帮助,但Spencer是正确的,你将通过Google学到更多关于属性的阅读,而不是我在这里可以解释的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM