Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is metadata in .NET?

Tags:

c#

.net

metadata

I googled several sites to understand what metadata is in .NET and it means.

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.

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?

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.

MSDN wrote 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?

I am completely confused.

like image 572
KMC Avatar asked Jan 14 '12 08:01

KMC


People also ask

What is metadata in C# with example?

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.

What is a metadata example?

A simple example of metadata for a document might include a collection of information like the author, file size, the date the document was created, and keywords to describe the document. Metadata for a music file might include the artist's name, the album, and the year it was released.

What do you mean by metadata?

Metadata represents data about data. Metadata enriches the data with information that makes it easier to find, use and manage. For instance, HTML tags define layout for human readers. Semantic metadata helps computers to interpret data by adding references to concepts in a knowledge graph.

What is metadata vb net?

Metadata is information stored in the assembly that describes the types and methods of the assembly and provides other useful information about the assembly. Assemblies are said to be self-describing because the metadata fully describes the contents of each module.


2 Answers

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.

You can build very simple console application and use ildasm.exe to 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 Hello to console:

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.

like image 192
k.m Avatar answered Sep 23 '22 23:09

k.m


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.

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

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.

like image 29
EMP Avatar answered Sep 19 '22 23:09

EMP