Skip to content

[netcoreapp2.0] 'AppDomain.CurrentDomain.GetAssemblies()' doesn't return the emitted dynamic assembly #21340

Closed
@daxian-dbw

Description

@daxian-dbw

AppDomain.CurrentDomain.GetAssemblies() doesn't return dynamically emitted assemblies.

Repro

using System;
using System.Reflection;
using System.Reflection.Emit;

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);

            bool isEmittedAssemblyFound = false;
            var allAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly item in allAssemblies)
            {
                if (item.FullName == ti.Assembly.FullName)
                {
                    isEmittedAssemblyFound = true;
                    Console.WriteLine("Found it");
                    break;
                }
            }

            if (!isEmittedAssemblyFound)
            {
                Console.WriteLine("AppDomain.CurrentDomain.GetAssemblies() doesn't return the emitted dynamic assemlby");
            }
        }
    }
}

Expected Result
The program prints Found it

Actual Result

Dynamic assembly emitted: Microsoft.PowerShell.Cmdletization.GeneratedTypes, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
AppDomain.CurrentDomain.GetAssemblies() doesn't return the emitted dynamic assemlby

Aren't dynamically emitted assembly loaded in the default assembly load context? Why AppDomain.CurrentDomain.GetAssemblies() not returning them?

Metadata

Metadata

Assignees

Labels

area-System.RuntimequestionAnswer questions and provide assistance, not an issue with source code or documentation.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions