C# Metadata-II


Metadata is binary information describing yourprogram that is stored either in a common language runtime portableexecutable (PE) file or in memory. When you compile your code into a PEfile, metadata is inserted into one portion of the file, while yourcode is converted to Microsoft intermediate language (MSIL) andinserted into another portion of the file. Every type and memberdefined and referenced in a module or assembly is described withinmetadata. When code is executed, the runtime loads metadata into memoryand references it to discover information about your code's classes,members, inheritance, and so on.

Metadata describes every type and memberdefined in your code/a module in a language-neutral fashion. Forinstance, by disassembling the System namespace using the ILDASMutility provided with the .net framework, we can disassemble all theclasses and types used in the namespace.

The following figure shows the manifest details of the System.dll file.

Metadata stores the following information in a PE (Portable Executable) file:

- Description of the assembly.
- Identity (name, version, culture, public key).
- The types that are exported.
- Other assemblies that this assembly depends on.
- Security permissions needed to run.
- Description of types.
- Name, visibility, base class, and interfaces implemented.
- Members (methods, fields, properties, events, nested types).
- Attributes
- Additional descriptive elements that modify types and members.

Metadata is the foundation for a simplerprogramming model. Metadata allows .NET objects to describe themselvesautomatically in a language-neutral manner, unseen by both thedeveloper and the user.

Your program's metadata contains everythingneeded to interact with another program or class or object. Since COMhas the inbuilt capability to provide for interfaces and communicationbetween objects, external API files or component references are notrequired, allowing you to use one file for both definition andimplementation. Runtime modules and assemblies do not requireregistration with the operating system but they do require recognizabletype patterns and compliance with the CLR.

Metadata is stored in one section of a .NETportable executable (PE) file while Microsoft intermediate language(MSIL) is stored in another section of the PE file. The metadataportion of the file contains a series of table and heap datastructures. The MSIL portion contains MSIL and metadata tokens thatreference the metadata portion of the PE file.

The above figure shows the disassembly of anamespace called "FindVowel", which finds the number of vowels in agiven string. The MSIL of the method getVowel(?) is disassembled anddisplayed in the yellow screen.

using System;
namespace FindVowel{

public class VowLetter{
public string s;
public int count;
public int Vowelctr;
public VowLetter()
{
s=null;
count=0;
Vowelctr=0;
}

public string getVowel(String str)
{
char ch;
int i=0;
bool flag=false;

while (flag==false)
{
ch=str[i];
switch(ch)
{
case 'a':
s+=ch.ToString();
break;
case 'A':
s+=ch.ToString();
break;
case 'e':
s+=ch.ToString();
break;
case 'E':
s+=ch.ToString();
break;
case 'o':
s+=ch.ToString();
break;
case 'O':
s+=ch.ToString();
break;
case 'u':
s+=ch.ToString();
break;
case 'U':
s+=ch.ToString();
break;
case '!':
flag=true;
break;
}
i++;
}
return s;}
}
class app
{
public static void Main()
{
VowLetter myob=new VowLetter();
app theOb=new app();
st=myob.getVowel("Hello World !");
Console.WriteLine(st.ToString());
}
}
}

By compiling the above program with
csc /r:System.dll /t:library /out:mydll.dll <nameofsourcefile.cs>
</nameofsourcefile.cs>

you can create a DLL file called "mydll.dll",which can then be imported into another program with the usingdirective. Of course, you must create a type library for the same, asall types must be CLS compliant or they become unmanaged code for theCLR.

The manifest of the DLL file, "MyDll.dll" andthe IL code of the getVowel(string) method is seen in the followingfigure, which shows the disassembly, as well as the metadata of the DLLfile.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!