Skip to content

[API Proposal]: MetadataReader.GetAssemblyName(string assemblyFile) #63960

@VSadov

Description

@VSadov

Background and motivation

As part of #63915 we are moving AssemblyName.GetAssemblyName(string assemblyFile) implementation to managed code, so we need a helper entry point in System.Reflection.Metadata.

Current implementation relies on VM assembly loader, but intentionally bypasses the internal caches so that the call has no lasting effects on the runtime state.
The implementation also needs to handle assemblies that do not match the current platform, which is an awkward situation for the assembly loader and require special handling.
As it is, every runtime (CoreClr, Mono, CoreRT) needs to implement AssemblyName.GetAssemblyName leading to inconsistencies in implementation. Providing a common managed implementation is very desirable.

Since this is all about PE/Assembly parsing, System.Reflection.Metadata appears to be a logical place for the implementation.

While internal helper could be sufficient, it is better if the helper is public. In fact the helper may become the primary/recommended way to fetch an assembly name.

API Proposal

namespace System.Reflection.Metadata
{
   public sealed partial class MetadataReader
    {
        // given a path to assembly returns its AssemblyName similarly to AssemblyName.GetAssemblyName
        // in fact AssemblyName.GetAssemblyName will use this implementation.
+       public static AssemblyName GetAssemblyName(string assemblyFile) { ... }
    }
}

== Exceptions:
AssemblyName.GetAssemblyName handles failure cases by throwing a range of exceptions. It may be difficult to match every error case precisely. Inconsistencies in the file format typically lead to BadImageFormatException.
There is less consistency for other issues like file/IO and there are some disagreements between platforms and different OSs.

== Proposal:
Map InvalidOperationException coming from MetadataReader to BadImageFormatException with the same message to minimize compat concerns. Propagate other exceptions as-is.

API Usage

using System.Reflection.Metadata;

AssemblyName assemblyName = MetadataReader.GetAssemblyName("Hello.dll");

Alternative Designs

It would be ok if the helper is internal, but measures will need to be taken that it is not trimmed by the linker and does not disappear in future/alternative implementations of System.Reflection.Metadata

Risks

Low. This is a new API entry point exposing existing implementation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions