System.Reflection.AssemblyName

Represents the information contained in an assembly manifest.

The AssemblyName class represents the information contained in an assembly manifest. This includes the name, version, culture, and public key token of an assembly. It is commonly used when loading assemblies dynamically or querying assembly information.

Syntax

public sealed class AssemblyName : ICloneable, System.Runtime.Serialization.ISerializable

Namespace: System.Reflection
Assembly: System.Runtime.dll

Inheritance

Object
AssemblyName

Implements

Properties

Methods

Remarks

The AssemblyName class provides a rich set of properties to describe an assembly. It is crucial for scenarios involving assembly loading, binding, and resolution within the .NET runtime.

Key uses include:

The FullName property provides a concise, definitive string representation that can be used for assembly identification.

Examples

Getting AssemblyName for the current assembly

using System;
using System.Reflection;

public class Example
{
    public static void Main(string[] args)
    {
        // Get the AssemblyName for the currently executing assembly
        AssemblyName currentAssemblyName = Assembly.GetExecutingAssembly().GetName();

        Console.WriteLine($"Assembly Name: {currentAssemblyName.Name}");
        Console.WriteLine($"Version: {currentAssemblyName.Version}");
        Console.WriteLine($"Full Name: {currentAssemblyName.FullName}");

        if (currentAssemblyName.CultureInfo != null)
        {
            Console.WriteLine($"Culture: {currentAssemblyName.CultureInfo.Name}");
        }
        else
        {
            Console.WriteLine("Culture: Neutral");
        }
    }
}

Creating an AssemblyName from a string

using System;
using System.Reflection;

public class Example
{
    public static void Main(string[] args)
    {
        // Example assembly name string
        string assemblyNameString = "MyLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";

        try
        {
            AssemblyName assemblyName = new AssemblyName(assemblyNameString);

            Console.WriteLine($"Parsed Name: {assemblyName.Name}");
            Console.WriteLine($"Parsed Version: {assemblyName.Version}");
            Console.WriteLine($"Parsed Full Name: {assemblyName.FullName}");

            if (assemblyName.CultureInfo != null)
            {
                Console.WriteLine($"Parsed Culture: {assemblyName.CultureInfo.Name}");
            }
            else
            {
                Console.WriteLine("Parsed Culture: Neutral");
            }
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine($"Error parsing assembly name: {ex.Message}");
        }
    }
}

Exceptions

ArgumentException: Thrown when the assembly name string is invalid or malformed.

FileNotFoundException: Thrown when an assembly cannot be found.

BadImageFormatException: Thrown when the assembly is not a valid executable assembly.