Metadata in .NET refers to the data about the types, members, and other components in an assembly. It provides important information that allows the .NET runtime and tools to understand the structure and behavior of your code. This includes information such as types, methods, properties, attributes, and other aspects that describe your application's components. Metadata is essential for reflection, debugging, and certain tasks like serialization or working with libraries and frameworks.
What is Metadata?
Where is Metadata Stored?
Metadata Components:
[Obsolete] attribute or [Serializable] attribute.Each assembly in .NET has its own set of metadata that describes the assembly itself, including:
en-US).This assembly metadata is often specified in the Assembly Information file (AssemblyInfo.cs) in .NET Framework projects or as part of the project file (.csproj) in .NET Core/.NET 5+ projects.
.csproj for .NET Core / .NET 5+):<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>MyApp</AssemblyName>
<Version>1.0.0</Version>
<Description>My first application</Description>
<Company>MyCompany</Company>
<Authors>Author Name</Authors>
</PropertyGroup>
</Project>
In this example, the Version, AssemblyName, Description, Company, and other properties will be embedded in the assembly metadata.
Reflection is a powerful feature in .NET that allows you to examine the metadata of assemblies at runtime. This is useful for tasks like dynamically loading types, accessing properties, or invoking methods.
System.Reflection namespace provides classes to inspect metadata in assemblies.using System;
using System.Reflection;
class Program
{
static void Main()
{
// Load an assembly
Assembly assembly = Assembly.LoadFrom("MyApp.dll");
// Get all types in the assembly
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
Console.WriteLine($"Type: {type.FullName}");
// Get methods of the type
MethodInfo[] methods = type.GetMethods();
foreach (MethodInfo method in methods)
{
Console.WriteLine($" Method: {method.Name}");
}
// Get properties of the type
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine($" Property: {property.Name}");
}
}
}
}
This code dynamically loads an assembly (MyApp.dll), and then reflects on its types, methods, and properties, printing them to the console.
In .NET, you can create custom attributes to add metadata to your types, methods, properties, etc. Custom attributes can be used to provide additional information about code elements, which can then be accessed at runtime via reflection.
using System;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class DocumentationAttribute : Attribute
{
public string Author { get; }
public string Description { get; }
public DocumentationAttribute(string author, string description)
{
Author = author;
Description = description;
}
}
[Documentation("John Doe", "This class does important work.")]
public class MyClass
{
[Documentation("Jane Smith", "This method calculates the result.")]
public int Calculate(int a, int b) => a + b;
}
class Program
{
static void Main()
{
Type type = typeof(MyClass);
var attributes = type.GetCustomAttributes(typeof(DocumentationAttribute), false);
foreach (DocumentationAttribute attr in attributes)
{
Console.WriteLine($"Class Author: {attr.Author}, Description: {attr.Description}");
}
MethodInfo methodInfo = type.GetMethod("Calculate");
var methodAttributes = methodInfo.GetCustomAttributes(typeof(DocumentationAttribute), false);
foreach (DocumentationAttribute attr in methodAttributes)
{
Console.WriteLine($"Method Author: {attr.Author}, Description: {attr.Description}");
}
}
}
In this example, the DocumentationAttribute is a custom attribute used to provide metadata about classes and methods. This metadata can be accessed via reflection at runtime.
You can view the metadata embedded in an assembly using several tools:
ILDasm (Intermediate Language Disassembler)ILDasm is a tool provided by the .NET SDK that can display the metadata of assemblies. You can open an assembly and inspect its metadata.
ildasm MyApp.dll
This will open the IL Disassembler window, allowing you to browse through the assembly's metadata, types, methods, and more.
While metadata is typically read-only, it can be modified using certain tools, such as:
By leveraging metadata, .NET developers can create flexible, dynamic, and maintainable code that adapts to changing requirements, while also ensuring that additional information can be easily accessed at runtime.
Open this section to load past papers