40

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.

KMC
  • 19,548
  • 58
  • 164
  • 253

6 Answers6

26

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.

k.m
  • 30,794
  • 10
  • 62
  • 86
  • 2
    +1 I have to take this as my answer because it makes reading of other answers make sense and clear my confusion. – KMC Jan 16 '12 at 09:30
19

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.

EMP
  • 59,148
  • 53
  • 164
  • 220
  • I read the Wiki, and I am confused. Metadata supposed to describe my code like a commend no? How could the computer understand my purpose and generate it for me? In HTML the meta tags are typed in manually with keyword and title to describe the page, the browser would not be intelligent enough to understand page content and generate keywords and title for me? man i'm all confused.. – KMC Jan 14 '12 at 08:48
  • so metadata captures identifiers for class, methods, variables etc. What's the point of this? Does getting the identifier name = describing my data? I though method signature also sum up the information in a method..again, even more confused. sorry. please help – KMC Jan 14 '12 at 09:12
  • Right, the method signature is part of metadata. Just to call the method it would be enough to know the binary offset of the method and its number and size of parameters. However, .NET stores the full signature: method name, return type, each parameter's exact type and name, any attributes on the method or parameters, etc. That's metadata. The point of it is to enable Reflection (and some other things). – EMP Jan 14 '12 at 09:46
12

This is a great and comprehensive article 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.

Reflection in dot net is a very powerful concept and it is based on reading the metadata stored along with the actual code.

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
7

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.

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.

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):

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.WriteLine in mscorlib.dll.

Only this part gets compiled into 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.

So it comes down to a way to separate the executable (imperative) IL code from the non-executable (declarative) parts.

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.

JacquesB
  • 41,662
  • 13
  • 71
  • 86
4

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.

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.

if this makes the interest in you refer--https://msdn.microsoft.com/en-us/library/xcd8txaw%28v=vs.110%29.aspx

Reeves
  • 726
  • 7
  • 13
4

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

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.

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)

Shaun Wilde
  • 8,228
  • 4
  • 36
  • 56
  • If I have a method "myMethod", how would metadata describes it? The compiler does not know what the method means and only execute it, how could the description be generated other than manually typed in by myself? Confused. – KMC Jan 14 '12 at 09:14
  • Exactly, the metadata is information such as the name of the type, name of the method, name of parameters, name of local variables ... – Shaun Wilde Jan 14 '12 at 21:28
  • 1
    Have a look at [ILSpy](http://wiki.sharpdevelop.net/ILSpy.ashx) to see metadata being put to use to interpret IL and return "equivalent" source code. – Shaun Wilde Jan 14 '12 at 21:34