在 C# 中,class 或函数前的方括号中写的是什么?
In C# what is the thing written in square brackets before a class or a function?
我熟悉C和C++。我是第一次使用 C#。我试图了解 WCF 和 WPF。我使用 CodeProject 中的教程。作者在那里给出了示例代码。他在方括号中的接口和方法之前写了一些东西。那些是什么?他们是评论吗?
这是给定的示例代码。
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof(IChatCallback))]
interface IChat
{
[OperationContract(IsOneWay = true, IsInitiating = false,
IsTerminating = false)]
void Say(string msg);
[OperationContract(IsOneWay = true, IsInitiating = false,
IsTerminating = false)]
void Whisper(string to, string msg);
[OperationContract(IsOneWay = false, IsInitiating = true,
IsTerminating = false)]
Person[] Join(Person name);
[OperationContract(IsOneWay = true, IsInitiating = false,
IsTerminating = true)]
void Leave();
}
这些是属性。
attribute 是一个声明性标签,用于向运行时传达有关各种元素行为的信息,例如 classes、方法、程序中的结构、枚举器、程序集等。您可以使用属性向程序添加声明性信息。声明性标签由位于其所用元素上方的方括号 ([ ]) 表示。
例如,属性可用于指示 class 是否可序列化,或 特定 属性 应写入数据库中的哪个字段到等等...
比如我们来看这个属性:
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
该属性是 OperationContract
. And IsOneWay
, IsInitiating
, IsTerminating
是该属性的特性。
OperationContract - Indicates that a method defines an operation that
is part of a service contract in a Windows Communication Foundation
(WCF) application.
IsOneWay - Gets or sets a value that indicates
whether an operation returns a reply message.
IsInitiating - Gets or
sets a value that indicates whether the method implements an operation
that can initiate a session on the server (if such a session exists).
IsTerminating - Gets or sets a value that indicates whether the
service operation causes the server to close the session after the
reply message, if any, is sent.
您可以使用预定义属性或创建自己的自定义属性。
我熟悉C和C++。我是第一次使用 C#。我试图了解 WCF 和 WPF。我使用 CodeProject 中的教程。作者在那里给出了示例代码。他在方括号中的接口和方法之前写了一些东西。那些是什么?他们是评论吗? 这是给定的示例代码。
[ServiceContract(SessionMode = SessionMode.Required,
CallbackContract = typeof(IChatCallback))]
interface IChat
{
[OperationContract(IsOneWay = true, IsInitiating = false,
IsTerminating = false)]
void Say(string msg);
[OperationContract(IsOneWay = true, IsInitiating = false,
IsTerminating = false)]
void Whisper(string to, string msg);
[OperationContract(IsOneWay = false, IsInitiating = true,
IsTerminating = false)]
Person[] Join(Person name);
[OperationContract(IsOneWay = true, IsInitiating = false,
IsTerminating = true)]
void Leave();
}
这些是属性。
attribute 是一个声明性标签,用于向运行时传达有关各种元素行为的信息,例如 classes、方法、程序中的结构、枚举器、程序集等。您可以使用属性向程序添加声明性信息。声明性标签由位于其所用元素上方的方括号 ([ ]) 表示。
例如,属性可用于指示 class 是否可序列化,或 特定 属性 应写入数据库中的哪个字段到等等...
比如我们来看这个属性:
[OperationContract(IsOneWay = true, IsInitiating = false, IsTerminating = false)]
该属性是 OperationContract
. And IsOneWay
, IsInitiating
, IsTerminating
是该属性的特性。
OperationContract - Indicates that a method defines an operation that is part of a service contract in a Windows Communication Foundation (WCF) application.
IsOneWay - Gets or sets a value that indicates whether an operation returns a reply message.
IsInitiating - Gets or sets a value that indicates whether the method implements an operation that can initiate a session on the server (if such a session exists).
IsTerminating - Gets or sets a value that indicates whether the service operation causes the server to close the session after the reply message, if any, is sent.
您可以使用预定义属性或创建自己的自定义属性。