diff --git a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs index a021153da1a5d..891daac1b52be 100644 --- a/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs +++ b/src/libraries/System.Reflection.DispatchProxy/src/System/Reflection/DispatchProxyGenerator.cs @@ -118,19 +118,9 @@ private sealed class ProxyAssembly [RequiresDynamicCode("Defining a dynamic assembly requires generating code at runtime")] public ProxyAssembly(AssemblyLoadContext alc) { - string name; - if (alc == AssemblyLoadContext.Default) - { - name = "ProxyBuilder"; - } - else - { - string? alcName = alc.Name; - name = string.IsNullOrEmpty(alcName) ? $"DispatchProxyTypes.{alc.GetHashCode()}" : $"DispatchProxyTypes.{alcName}"; - } AssemblyBuilderAccess builderAccess = alc.IsCollectible ? AssemblyBuilderAccess.RunAndCollect : AssemblyBuilderAccess.Run; - _ab = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(name), builderAccess); + _ab = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("ProxyBuilder"), builderAccess); _mb = _ab.DefineDynamicModule("testmod"); } diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs index fd0545c964866..9609176318754 100644 --- a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs +++ b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs @@ -811,6 +811,25 @@ static object CreateTestDispatchProxy(Type type, bool useGenericCreate) } } + [Fact] + public static void Test_Multiple_AssemblyLoadContextsWithBadName() + { + if (typeof(DispatchProxyTests).Assembly.Location == "") + return; + + Assembly assembly = Assembly.LoadFile(typeof(DispatchProxyTests).Assembly.Location); + Type type = assembly.GetType(typeof(DispatchProxyTests).FullName); + MethodInfo method = type.GetMethod(nameof(Demo), BindingFlags.NonPublic | BindingFlags.Static); + Assert.True((bool)method.Invoke(null, null)); + } + + internal static bool Demo() + { + TestType_IHelloService proxy = DispatchProxy.Create(); + proxy.Hello("Hello"); + return true; + } + private static TInterface CreateHelper(bool useGenericCreate) where TProxy : DispatchProxy { if (useGenericCreate)