ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCSI-415Metadata
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CSI-415

    Advanced Programming

    Progress0 / 55 done
    Topics55
    1Visual Programming Basics2Introduction to Events3Fundamentals of Event-Driven Programming4Message Handling5User Interfaces6Graphics Device Interface7Painting and Drawing8Windows Management9Input Devices10Resources11String and Menu Resource12Dialogs and Windows Controls13Common Controls14Dynamic Link Libraries (DLLs)15Threads and Synchronization16Network Programming17Building Class Libraries at the Command Line18Class Libraries19Using References20Assemblies21Private Assembly Deployment22Shared Assembly Deployment23Configuration Overview24Configuration Files25Programmatic Access to Configuration26Using SDK Tools for Signing and Deployment27Metadata28Reflection29Late Binding30Directories and Files31Serialization32Attributes33Memory Management and Garbage Collection34Threading and Synchronization35Asynchronous Delegates36Application Domains37Marshal by Value38Marshal by Reference39Authentication and Authorization40Configuring Security41Code Access Security42Code Groups43Evidence44Permissions45Role-Based Security46Principals and Identities47Using Data Readers48Using Data Sets49Interacting with XML Data50Tracing Event Logs51Using the Boolean Switch and Trace Switch Classes52Print Debugging Information with the Debug Class53Instrumenting Release Builds with the Trace Class54Using Listeners55Implementing Custom Listeners
    7 min read
    Topic 27 of 55
    Topic Study Note Illustration
    27

    Metadata

    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...

    Metadata in .NET

    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.

    Key Aspects of Metadata in .NET:

    1. What is Metadata?

      • Metadata is data that describes the types, methods, and other structures in an assembly.
      • It is embedded into the Assembly when the .NET compiler compiles the source code.
      • It’s separate from the code itself, and is mainly used for runtime operations, reflection, and dynamic loading of types and assemblies.
    2. Where is Metadata Stored?

      • Metadata is embedded in the PE (Portable Executable) files (i.e., DLL or EXE) produced during compilation. These files contain both IL (Intermediate Language) code and metadata.
      • The metadata is contained in the CLI header and metadata tables within the assembly file.
    3. Metadata Components:

      • Types: Information about the classes, interfaces, and enums defined in the assembly.
      • Methods: Metadata about the methods, including method names, parameters, return types, etc.
      • Fields: Information about the fields or properties in a class, including types and attributes.
      • Attributes: Metadata that provides additional information about types, methods, and fields. For example, the [Obsolete] attribute or [Serializable] attribute.
      • References: Information about references to other assemblies that the current assembly depends on.
      • Custom Metadata: You can define custom metadata using attributes.

    1. Assembly Metadata

    Each assembly in .NET has its own set of metadata that describes the assembly itself, including:

    • Assembly Name: The name of the assembly.
    • Version: The version number of the assembly (major, minor, build, revision).
    • Culture: Defines the culture or localization of the assembly (e.g., en-US).
    • Public Key Token: A hash of the public key used in strong-naming the assembly.

    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.

    Example (in .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.


    2. Reflection and 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.
    • Reflection allows you to inspect types, methods, properties, attributes, and more.

    Example of Using Reflection to Get Metadata:

    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.


    3. Custom Attributes in Metadata

    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.

    Example of a Custom Attribute:

    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.


    4. Common Metadata Use Cases

    • Versioning: Metadata is often used for version control of assemblies. The assembly version in metadata allows the runtime to ensure compatibility between different versions of an assembly.
    • Reflection: Reflection is commonly used in frameworks and libraries (such as ORMs or Dependency Injection frameworks) to discover types, properties, and methods dynamically.
    • Serialization: Metadata often provides the necessary information for serialization and deserialization frameworks to correctly convert objects to and from various formats (e.g., JSON, XML).
    • Custom Attributes: Used for adding business-specific metadata to code elements (e.g., marking methods as deprecated or making classes serializable).

    5. Viewing Metadata

    You can view the metadata embedded in an assembly using several tools:

    • ILSpy: A popular .NET decompiler that allows you to view metadata, IL code, and more.
    • dotPeek: A JetBrains tool that decompiles .NET assemblies and lets you inspect metadata.
    • Reflector: Another decompiler that helps in inspecting assemblies and metadata.
    • PE Explorer: Allows you to view the PE headers and metadata of executable files.

    Using 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.


    6. Modifying Metadata

    While metadata is typically read-only, it can be modified using certain tools, such as:

    • Mono.Cecil: A library for reading and writing .NET assemblies, including modifying metadata.
    • System.Reflection.Emit: Allows you to dynamically create or modify types and methods in metadata during runtime.

    7. Summary

    • Metadata in .NET is crucial for describing the structure of assemblies, types, methods, and other components.
    • It is stored in the assembly file and used at runtime for operations like reflection and dynamic loading.
    • Reflection allows you to access and inspect metadata programmatically.
    • You can add custom metadata via attributes, which can be inspected at runtime.
    • Tools like ILSpy, dotPeek, and ILDasm help you view and analyze assembly metadata.
    • Metadata is often used for versioning, serialization, and reflection.

    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.

    ← Previous topic 26
    Using SDK Tools for Signing and Deployment
    Next topic 28 →
    Reflection

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Programming