I have seen a lot of C# programs that use the []
, for example [STAThread]
and then the code follows. Another classic example is [DLLImport]
.
我见过很多使用[]的C#程序,例如[STAThread],然后代码如下。另一个经典的例子是[DLLImport]。
I know what STAThread
means but my question is what is the significance of the square brackets, essentially what do they tell the compiler?
我知道STAThread的意思,但我的问题是方括号的意义是什么,基本上它们告诉编译器的是什么?
5 个解决方案
#1
It's an attribute. Attributes are a form of metadata that you can attach to various code elements: classes, methods, assemblies etc.
这是一个属性。属性是元数据的一种形式,您可以将其附加到各种代码元素:类,方法,程序集等。
Some attributes have special meaning to the C# compiler, for instance the [Serializable]
probably tells the compiler to emit some code that can serialize an instance of the class (I say 'probably' since I do not know the inner workings of the C# compiler).
一些属性对C#编译器有特殊意义,例如[Serializable]可能告诉编译器发出一些可以序列化类实例的代码(我说'可能',因为我不知道C#编译器的内部工作原理)。
You can also create your own attributes (by inheriting System.Attribute
). Using reflection you could then at run-time extract information from the attributes.
您还可以创建自己的属性(通过继承System.Attribute)。然后使用反射,您可以在运行时从属性中提取信息。
A simple example would be to create an attribute to specify what kind of input field to use in a HTML form when displaying an object's property.
一个简单的例子是创建一个属性,以指定在显示对象属性时在HTML表单中使用哪种输入字段。
Some links:
#2
These are attributes.
这些是属性。
Attributes have many uses - [Obsolete]
marks a method as obsolete and the compiler will warn you. Others like [DebuggerNonUserCode]
tell nothing to the compiler and are there to let the debugger know that the code in the marked method is auto-generated.
属性有很多用途 - [已废弃]将方法标记为过时,编译器会警告您。像[DebuggerNonUserCode]这样的其他人对编译器一无所知,让调试器知道标记方法中的代码是自动生成的。
You can also create your own attributes and use them to mark any kind of metadata. For example, your Customer object might have an attribute [MarketingInformation("Customer is rich! Milk him good!")].
您还可以创建自己的属性并使用它们来标记任何类型的元数据。例如,您的Customer对象可能具有一个属性[MarketingInformation(“客户很丰富!给他好好喝!”)]。
#3
See here for info about attributes in .Net:
有关.Net中的属性的信息,请参阅此处:
#4
They are attributes, that add meta data to whatever they are decorating.
它们是属性,可以将元数据添加到它们正在装饰的任何内容中。
#5
Theses are called code attributes. Attributes are used to mark code with properties which are usually designed to specify behavior during execution. They are commonly used to mark methods, properties and parameters. During execution of your code something called "reflection" will be performed to examine the code. Reflection tells the compiler to observe and obey any instructions specified by you as the coder marking attributes against the code.
这些被称为代码属性。属性用于标记具有属性的代码,这些属性通常用于指定执行期间的行为。它们通常用于标记方法,属性和参数。在执行代码期间,将执行称为“反射”的操作来检查代码。 Reflection告诉编译器观察并遵守您指定的任何指令作为编码器标记代码的属性。
A good example would be the [Serializable] attribute. This attribute when marked above a class indicates to the compiler that it can be serialized for the purposes of persisting the class instance or for transmitting across a medium such as SOAP web services.
一个很好的例子是[Serializable]属性。当在类上方标记时,该属性向编译器指示它可以被序列化以便持久化类实例或者通过诸如SOAP web服务之类的介质进行传输。
See the following article: link text
请参阅以下文章:链接文本
#1
It's an attribute. Attributes are a form of metadata that you can attach to various code elements: classes, methods, assemblies etc.
这是一个属性。属性是元数据的一种形式,您可以将其附加到各种代码元素:类,方法,程序集等。
Some attributes have special meaning to the C# compiler, for instance the [Serializable]
probably tells the compiler to emit some code that can serialize an instance of the class (I say 'probably' since I do not know the inner workings of the C# compiler).
一些属性对C#编译器有特殊意义,例如[Serializable]可能告诉编译器发出一些可以序列化类实例的代码(我说'可能',因为我不知道C#编译器的内部工作原理)。
You can also create your own attributes (by inheriting System.Attribute
). Using reflection you could then at run-time extract information from the attributes.
您还可以创建自己的属性(通过继承System.Attribute)。然后使用反射,您可以在运行时从属性中提取信息。
A simple example would be to create an attribute to specify what kind of input field to use in a HTML form when displaying an object's property.
一个简单的例子是创建一个属性,以指定在显示对象属性时在HTML表单中使用哪种输入字段。
Some links:
#2
These are attributes.
这些是属性。
Attributes have many uses - [Obsolete]
marks a method as obsolete and the compiler will warn you. Others like [DebuggerNonUserCode]
tell nothing to the compiler and are there to let the debugger know that the code in the marked method is auto-generated.
属性有很多用途 - [已废弃]将方法标记为过时,编译器会警告您。像[DebuggerNonUserCode]这样的其他人对编译器一无所知,让调试器知道标记方法中的代码是自动生成的。
You can also create your own attributes and use them to mark any kind of metadata. For example, your Customer object might have an attribute [MarketingInformation("Customer is rich! Milk him good!")].
您还可以创建自己的属性并使用它们来标记任何类型的元数据。例如,您的Customer对象可能具有一个属性[MarketingInformation(“客户很丰富!给他好好喝!”)]。
#3
See here for info about attributes in .Net:
有关.Net中的属性的信息,请参阅此处:
#4
They are attributes, that add meta data to whatever they are decorating.
它们是属性,可以将元数据添加到它们正在装饰的任何内容中。
#5
Theses are called code attributes. Attributes are used to mark code with properties which are usually designed to specify behavior during execution. They are commonly used to mark methods, properties and parameters. During execution of your code something called "reflection" will be performed to examine the code. Reflection tells the compiler to observe and obey any instructions specified by you as the coder marking attributes against the code.
这些被称为代码属性。属性用于标记具有属性的代码,这些属性通常用于指定执行期间的行为。它们通常用于标记方法,属性和参数。在执行代码期间,将执行称为“反射”的操作来检查代码。 Reflection告诉编译器观察并遵守您指定的任何指令作为编码器标记代码的属性。
A good example would be the [Serializable] attribute. This attribute when marked above a class indicates to the compiler that it can be serialized for the purposes of persisting the class instance or for transmitting across a medium such as SOAP web services.
一个很好的例子是[Serializable]属性。当在类上方标记时,该属性向编译器指示它可以被序列化以便持久化类实例或者通过诸如SOAP web服务之类的介质进行传输。
See the following article: link text
请参阅以下文章:链接文本