'[]' 之间的代码是什么以及它的作用是什么?

What is code between '[]' and what does it do?

我学习 C# 有一段时间了。写了一些程序等。我没有处理过高级概念,但是在研究其他人的代码时,我注意到有些代码行我不明白它们为什么在那里。在这个例子中,我知道我知道它正在导入一个 DLL,但我不确定这行代码叫什么?是声明吗?我在哪里可以了解更多关于它们的信息?

在不知道它叫什么的情况下试图了解它是不可能的:)

示例:

[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

but am not sure what this line of code is called?

[ ] 中的任何内容都是 Attribute 提供编译器或 运行 程序使用反射获得 insights/hints 提供 对象在其([])声明之后的视图。

有关介绍,请参阅 Attributes Tutorial

也许this link对你有用。

页面中关于属性是什么的相关段落在这里:

Attribute Basics

Attributes are generally applied physically in front of type and type member declarations. They're declared with square brackets, "[" and "]", surrounding the attribute such as the following ObsoleteAttribute attribute:

[ObsoleteAttribute] The "Attribute" part of the attribute name is optional. So the following is equivalent to the attribute above:

[Obsolete] You'll notice that the attribute is declared with only the name of the attribute, surrounded by square brackets. Many attributes have parameter lists, that allow inclusion of additional information that customizes a program even further. Listing 16.1 shows various ways of how to use the ObsoleteAttribute attribute.

Microsoft's MSDN page 上,有对该特定属性的使用说明。

The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. For instance, your managed application might need to make calls to the unmanaged WIN32 API.

如需更深入的信息,请在 Google 或 canonical sources on MSDN 上进行简单搜索,这将说明用途、用法和代码示例。