Skip to content

[API Proposal]: Expose Assembly.GetSimpleName() #118678

@Youssef1313

Description

@Youssef1313

Background and motivation

Today, getting assembly simple name is done via assembly.GetName().Name. This does more work than necessary, like GetCodeBase, GetPublicKey, GetVersion, and allocating AssemblyName instance. In a hot code path, this can be problematic.

API Proposal

namespace System.Reflection;

public abstract partial class Assembly
{
+   public virtual string GetSimpleName() => GetName().Name;
}

internal sealed partial class RuntimeAssembly : Assembly
{
-       internal string GetSimpleName()
+       public override string GetSimpleName()
        {
            RuntimeAssembly runtimeAssembly = this;
            string? name = null;
            GetSimpleName(new QCallAssembly(ref runtimeAssembly), new StringHandleOnStack(ref name));
            return name!;
        }
}

internal class DelegatingAssembly : Assembly
{
+       public override string GetSimpleName() => UnderlyingAssembly.GetSimpleName();
}

internal sealed partial class RuntimeAssemblyBuilder : AssemblyBuilder
{
+       public override string GetSimpleName() => InternalAssembly.GetSimpleName();
}

public sealed class PersistedAssemblyBuilder : AssemblyBuilder
{
+       public override string GetSimpleName() => _assemblyName.Name;
}

API Usage

Example:

_ = typeof(MyType).Assembly.GetSimpleName();

// Instead of:
_ = typeof(MyType).Assembly.GetName().Name;

Alternative Designs

Do nothing, and callers who have perf issues can introduce caching themselves.

The perf issue is more likely to be "querying the same assembly/assemblies over and over again" and not "querying too many different assemblies".

This was motivated by the perf investigations found in microsoft/vstest#15259 though.

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions