C# .NET 中的元数据是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8861065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is metadata in .NET?
提问by KMC
I googled several sites to understand what metadatais in .NET and it means.
我搜索了几个站点以了解metadata.NET 中的内容及其含义。
I'm still new to C# WPF desktop application programming. Back when I was web programming, there are meta tag in HTML code where we describe the site with titles, keywords and such. Is also similar in .NET application? I read wiki, and googled but all I get is conceptional explanation.
我还是 C# WPF 桌面应用程序编程的新手。当我在做 Web 编程时,HTML 代码中有元标记,我们用标题、关键字等来描述站点。.NET 应用程序中也类似吗?我阅读维基,并用谷歌搜索,但我得到的只是概念性解释。
One describes "metadata is data that describes the state of the assembly and a detailed description of each type, attribute within the assembly". Is metadata just a concept or something physical like line of codes I typed in somewhere to describe my code? If so, do my commend becomes my metadata?
一描述"metadata is data that describes the state of the assembly and a detailed description of each type, attribute within the assembly"。元数据只是一个概念还是一些物理的东西,比如我在某处键入来描述我的代码的代码行?如果是这样,我的推荐会成为我的元数据吗?
I read metadata is "Within the Common Language Runtime (CLR)", but I code only in C#, how can I code in CLR into the metadata? Is metadata a commend in CLR? How Can I change it.
我读取的元数据是"Within the Common Language Runtime (CLR)",但我只用 C# 编码,我如何在 CLR 中编码到元数据中?元数据在 CLR 中值得称赞吗?我怎样才能改变它。
MSDNwrote that metadata is binary information for software component of another language to understand it. I though only human needs description (commend) in English to understand what a block of code does. Software component simply executes whatever statement we wrote - what's is the need of the "binary" information. How can the compiler understand the meaning of my high level code to generate "Description of assembly"? If I write a program that convert currency, would the metadata auto-generated knowing the program is converting currency? Where is this intelligence?
MSDN写道元数据是另一种语言的软件组件理解它的二进制信息。我虽然只需要人类需要用英语进行描述(推荐)才能理解代码块的作用。软件组件只是执行我们编写的任何语句 - 需要“二进制”信息。编译器如何理解我的高级代码的含义以生成“程序集描述”?如果我编写一个转换货币的程序,元数据是否会在知道该程序正在转换货币的情况下自动生成?这个智慧在哪里?
I am completely confused.
我完全糊涂了。
采纳答案by k.m
Since others already provided great explanatory answers, I'll just mention how you can view metadata yourself.
由于其他人已经提供了很好的解释性答案,我将仅提及您如何自己查看元数据。
In your Microsoft SDK directory (most likely variations of C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools) there's program called ildasm.exe- it's simple disassembler that allows you to view compiled .NET binaries.
在您的 Microsoft SDK 目录(最有可能是C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools 的变体)中有一个名为的程序ildasm.exe- 它是一个简单的反汇编程序,允许您查看已编译的 .NET 二进制文件。
You can build very simple console application and use ildasm.exeto view compiled contents.
View/MetaInfo/Show!command (or simply Ctrl + M) will display metadata - you can check how they look like. Part of metadata from application printing Helloto console:
您可以构建非常简单的控制台应用程序并用于ildasm.exe查看编译的内容。
查看/元信息/显示!命令(或简单的Ctrl + M)将显示元数据 - 您可以检查它们的外观。从应用程序打印Hello到控制台的部分元数据:
TypeDef #1 (02000002)
-------------------------------------------------------
TypDefName: Program (02000002)
Flags : [Public] [AutoLayout] [Class] [AnsiClass] [BeforeFieldInit](00100001)
Extends : 01000001 [TypeRef] System.Object
Method #1 (06000001) [ENTRYPOINT]
-------------------------------------------------------
MethodName: Main (06000001)
Flags : [Public] [Static] [HideBySig] [ReuseSlot] (00000096)
RVA : 0x00002050
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
ReturnType: Void
1 Arguments
Argument #1: SZArray String
1 Parameters
(1) ParamToken : (08000001) Name : args flags: [none] (00000000)
Here you can see type definition (Program) and one of its methods (Main), which takes single input argument and returns void. This is naturally only part of metadata, even for simpliest programs there's a lot more.
在这里您可以看到类型定义 ( Program) 及其方法之一 ( Main),它接受单个输入参数并返回 void。这自然只是元数据的一部分,即使是最简单的程序也有更多。
回答by EMP
If you're familiar with .NET Reflection you can think of metadata as "the data that Reflection accesses". Each .NET assembly stores information about what types and methods it contains, the attributes on those methods, etc. It wouldn't need to store that just to run the code (native EXEs don't have that kind of information), but it needs it for other purposes, like enforcing declarative security and enabling Reflection.
如果您熟悉 .NET 反射,您可以将元数据视为“反射访问的数据”。每个 .NET 程序集存储有关它包含哪些类型和方法、这些方法的属性等的信息。它不需要存储这些信息只是为了运行代码(本机 EXE 没有这种信息),但它需要它用于其他目的,例如强制声明安全性和启用反射。
So metadata is "something physical", but most of it is automatically generated from the code you write. Adding attributes to your classes or methods is probably the only way you can directly change metadata. In particular, your source code comments will not be stored in the assembly as metadata (or in any other way).
所以元数据是“物理的东西”,但大部分是从你编写的代码中自动生成的。向类或方法添加属性可能是直接更改元数据的唯一方法。特别是,您的源代码注释不会作为元数据(或以任何其他方式)存储在程序集中。
The Wikipedia page on this is pretty good: http://en.wikipedia.org/wiki/.NET_metadata
关于这个的维基百科页面非常好:http: //en.wikipedia.org/wiki/.NET_metadata
Edit:No, metadata is not like comments. It is simply "data about the code", which is not part of the code itself (not needed to run the program). It's not like the HTML metadata at all. An example of metadata is the fact that the assembly contains a class named "MyClass" and that class contains a method named "DoSomething" with certain parameters, etc. So it's nothing mysterious - just "obvious" stuff mainly.
编辑:不,元数据不像评论。它只是“关于代码的数据”,它不是代码本身的一部分(不需要运行程序)。它根本不像 HTML 元数据。元数据的一个例子是,程序集包含一个名为“MyClass”的类,该类包含一个名为“DoSomething”的方法,带有某些参数等。所以它并不神秘——主要是“显而易见”的东西。
回答by Haris Hasan
This is a great and comprehensivearticle about meta data in dot net. Take a look at it. I hope it will clear many things. It has link to a page explaining how meta data is used at runtime.
这是一篇关于 dot net 元数据的伟大而全面的文章。看一看。我希望它会清除很多东西。它有一个页面链接,解释如何在运行时使用元数据。
Reflectionin dot net is a very powerful concept and it is based on reading the metadatastored along with the actual code.
回答by Shaun Wilde
Simply, Metadata is information that is stored about your program that you can examine by a number of means one, in .NET one of these methods is commonly referred to as Reflection
简而言之,元数据是存储的有关您的程序的信息,您可以通过多种方式进行检查,其中一种方法在 .NET 中通常称为反射
Metadata describes types (e.g. classes, interfaces, ...), methods and parameters (names and types) and attributes that have been applied. You can use this information in a number of ways e.g. test systems such as nunit and msstest (amongst others) use the metadata to 'discover' the tests within an assembly; other ways that metadata can be used is in databinding.
元数据描述类型(例如类、接口等)、方法和参数(名称和类型)以及已应用的属性。您可以通过多种方式使用此信息,例如 nunit 和 msstest(以及其他)等测试系统使用元数据来“发现”程序集中的测试;可以使用元数据的其他方式是数据绑定。
There are so many ways to use it - they even have books on it (well sections at least http://my.safaribooksonline.com/book/programming/csharp/9781449379629/reflection-and-metadata/729)
有很多方法可以使用它 - 他们甚至有关于它的书籍(至少http://my.safaribooksonline.com/book/programming/csharp/9781449379629/reflection-and-metadata/729)
回答by Reeves
don't make it complicated it's just ---Data(information) about Data.
不要让它变得复杂它只是---关于数据的数据(信息)。
just think about the Meta tag in HTML, it hold information about page, keyword, author, last modified. it means it hold information about a data that is your html page.
想想 HTML 中的 Meta 标签,它包含有关页面、关键字、作者、上次修改的信息。这意味着它保存有关作为您的 html 页面的数据的信息。
When we talk in terms of C#, Metadata is stored in one section of a .NET Framework portable executable (PE) file, while Microsoft intermediate language (MSIL) is stored in another section of the PE file. The metadata portion of the file contains a series of table and heap data structures. The MSIL portion contains MSIL and metadata tokens that reference the metadata portion of the PE file. Each metadata table holds information about the elements of your program. For example, one metadata table describes the classes in your code, another table describes the fields, and so on. If you have ten classes in your code, the class table will have tens rows, one for each class. Metadata tables reference other tables and heaps. For example, the metadata table for classes references the table for methods. Metadata also stores information in four heap structures: string, blob, user string, and GUID. All the strings used to name types and members are stored in the string heap. For example, a method table does not directly store the name of a particular method, but points to the method's name stored in the string heap.
当我们谈论 C# 时,元数据存储在 .NET Framework 可移植可执行 (PE) 文件的一个部分中,而 Microsoft 中间语言 (MSIL) 存储在 PE 文件的另一部分中。文件的元数据部分包含一系列表和堆数据结构。MSIL 部分包含引用 PE 文件元数据部分的 MSIL 和元数据标记。每个元数据表都包含有关程序元素的信息。例如,一个元数据表描述代码中的类,另一个表描述字段,等等。如果您的代码中有十个类,类表将有十行,每个类一行。元数据表引用其他表和堆。例如,类的元数据表引用了方法的表。元数据还将信息存储在四种堆结构中:字符串、blob、用户字符串和 GUID。用于命名类型和成员的所有字符串都存储在字符串堆中。例如,方法表并不直接存储特定方法的名称,而是指向存储在字符串堆中的方法名称。
if this makes the interest in you refer--https://msdn.microsoft.com/en-us/library/xcd8txaw%28v=vs.110%29.aspx
如果这让您感兴趣,请参考 - https://msdn.microsoft.com/en-us/library/xcd8txaw%28v=vs.110%29.aspx
回答by JacquesB
Metadata is parts of the information from the source code itself which is stored in a special section in the assembly when compiled. It is really an implementation detail in how assemblies are structured. For typical C# application development you don't really need to know about this. It is mostly relevant if you develop developer tools.
元数据是源代码本身信息的一部分,编译时存储在程序集中的特殊部分。它实际上是程序集结构的实现细节。对于典型的 C# 应用程序开发,您实际上不需要了解这一点。如果您开发开发人员工具,这主要是相关的。
The term "metadata" is somewhat misleading. Assembly metadata includes stuff from the code like constants and string literals which is not really metadata in the usual sense of the word. A more correct term would perhaps be non-executable data.
术语“元数据”有些误导。程序集元数据包括来自代码的内容,如常量和字符串文字,它们并不是通常意义上的元数据。更正确的术语可能是不可执行数据。
When C# is compiled into an assembly, the compilation output is separated into two sections. The IL which is the actual executable code in bytecode format, and the "metadata" which is all the other stuff: type, interface, and member declarations, method signatures, constants, external dependencies and so on.
当 C# 被编译成程序集时,编译输出被分成两部分。IL 是字节码格式的实际可执行代码,“元数据”是所有其他内容:类型、接口和成员声明、方法签名、常量、外部依赖等。
Take this program:
拿这个程序:
class Program
{
public static void Main(string[] args)
{
var x = 2 + 2;
Console.WriteLine("Hello World!");
}
}
When this program is compiled into an assembly, it is separated into metadata and IL. The metadata contains these declarations (represented in a language-independent binary format):
当这个程序被编译成一个程序集时,它被分成元数据和IL。元数据包含以下声明(以独立于语言的二进制格式表示):
class Program
{
public static void Main(string[] args);
}
Furthermore metadata contains the string literal "Hello World!", and the information that the assembly references System.Console.WriteLinein mscorlib.dll.
此外,元数据包含字符串文字"Hello World!"以及程序集System.Console.WriteLine在 中引用的信息mscorlib.dll。
Only this part gets compiled into IL:
只有这部分被编译成 IL:
var x = 2 + 2;
Console.WriteLine("Hello World!");
With the caveat that the method reference and the literal string are represented in the IL as pointers into the metadata. On the other hand the method declarations in the metadata have pointers into the IL to the code which implement the method body.
需要注意的是,方法引用和文字字符串在 IL 中表示为指向元数据的指针。另一方面,元数据中的方法声明具有指向 IL 的指针,指向实现方法主体的代码。
So it comes down to a way to separate the executable (imperative) IL code from the non-executable (declarative) parts.
因此,它归结为一种将可执行(命令性)IL 代码与不可执行(声明性)部分分开的方法。
Why is this separation useful? Because it allows tools to extract and use the metadata without having to actually execute any of the IL. For example Visual Studio is able to provide code completion to members defined in an assembly just by reading the metadata. The compiler can check that methods called from other assemblies actually exists and parameters match and so on.
为什么这种分离有用?因为它允许工具提取和使用元数据,而无需实际执行任何 IL。例如,Visual Studio 能够仅通过读取元数据为程序集中定义的成员提供代码完成。编译器可以检查从其他程序集调用的方法是否实际存在以及参数是否匹配等等。

