方括号 [] 在下面的代码中是什么意思? [英] What does square bracket [] mean in the below code?

查看:21
本文介绍了方括号 [] 在下面的代码中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 http://msdn.microsoft.com/en-us/library/dd584174(office.11​​).aspx 用于在 webpart 工具窗格中添加自定义属性.方括号 ([]) 在下面的代码中是什么意思?

I got below code from http://msdn.microsoft.com/en-us/library/dd584174(office.11).aspx for adding custom property in webpart tool pane. 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;
            }
        }

推荐答案

正如@Spencer Ruport 所说,它们是属性.它们在 .NET 中用于声明式编程.

As @Spencer Ruport said, they're attributes. They're used within .NET for declarative programming.

您可以在 MSDN 上找到有关每个属性的信息.但是,您应该知道属性的名称可以缩短.例如,在您的情况下,Category 是类名 CategoryAttribute 的缩写形式,而 XmlElement 是类名的缩写形式 XmlElementAttribute.声明属性时,类名的Attribute部分可以省略.

You can find information on each of these attributes at 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. When declaring attributes, the Attribute portion of the class name can be left out.

我已经将这些属性中的大部分与 PropertyGrid 控件结合使用(参见 here 示例),但在您的情况下,它们用于 Web 部件属性窗格.目的还是一样的.控件使用这些属性来了解如何向用户显示该属性.通过使用控件理解的各种属性的组合,可以声明性规定这种行为.

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. 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.

我希望这会有所帮助,但 Spencer 是正确的,您将通过 Google 了解更多有关属性的阅读资料,而我无法在此解释.

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.

这篇关于方括号 [] 在下面的代码中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆