Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
daxian-dbw opened this issue Apr 24, 2017 · 7 comments
Assignees
Labels
area-System.Runtime question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@daxian-dbw
Copy link
Contributor

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?

@daxian-dbw
Copy link
Contributor Author

@gkhanna79 is this a bug or another by-design behavior change?

@karelz
Copy link
Member

karelz commented Apr 25, 2017

@gkhanna79 who can answer it?

@gkhanna79
Copy link
Member

Aren't dynamically emitted assembly loaded in the default assembly load context?

Dynamically emitted assemblies are loaded in the load context of the assembly that emitted them. Which LoadContext does your repro emit them in?

@daxian-dbw
Copy link
Contributor Author

Which LoadContext does your repro emit them in?

It's in the default load context. You can see from the repro snippet, no additional load context is involved.

@gkhanna79
Copy link
Member

@daxian-dbw Do you get the expected behavior on Desktop?

@daxian-dbw
Copy link
Contributor Author

Yes, AppDomain.CurrentDomain.GetAssemblies returns the dynamic assembly as expected on Desktop.

F:\temp>test.exe
Dynamic assembly emitted: Microsoft.PowerShell.Cmdletization.GeneratedTypes, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Found it

@gkhanna79 gkhanna79 self-assigned this Apr 26, 2017
@gkhanna79
Copy link
Member

PR with fix is out.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.0.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

4 participants