Skip to content

[netcoreapp2.0] AssemblyLoadContext.GetLoadContext crashes when pass in a dynamic assembly #21386

Closed

Description

@daxian-dbw

AssemblyLoadContext.GetLoadContext crashes the running program when a dynamic assembly is passed in as an argument.

Repro

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.Loader;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            AssemblyName aName = new AssemblyName("Microsoft.PowerShell.Cmdletization.GeneratedTypes");
            AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
            ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);

            string fullEnumName = "Microsoft.PowerShell.Cmdletization.GeneratedTypes.TestEnum";
            Type underlyingType = typeof(Int32);
            EnumBuilder eb = mb.DefineEnum(fullEnumName, TypeAttributes.Public, underlyingType);

            eb.DefineLiteral("Single", 0);
            eb.DefineLiteral("Multiple", 1);

            TypeInfo ti = eb.CreateTypeInfo();

            Console.WriteLine("Dynamic assembly emitted: {0}", ti.Assembly.FullName);

            var loadContext = AssemblyLoadContext.GetLoadContext(ti.Assembly);
            Console.WriteLine("Yay"); // <---- program crashes and this line never executes
        }
    }
}

Expected behavior
The default load context should be returned by AssemblyLoadContext.GetLoadContext

Actual behavior

d:\cmdlet\Sample>dotnet run
Dynamic assembly emitted: Microsoft.PowerShell.Cmdletization.GeneratedTypes, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

d:\cmdlet\Sample>

I think this bug is related to #21340

/cc @gkhanna79

Activity

  1. self-assigned this
    on Apr 26, 2017
  2. gkhanna79 commented on Apr 26, 2017

    @gkhanna79
    Member
  3. transferred this issue fromdotnet/corefxon Jan 31, 2020
  4. added this to the 2.0.0 milestone on Jan 31, 2020
  5. ghost locked as resolved and limited conversation to collaborators on Dec 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions