Skip to content

Commit

Permalink
Do not use ALC name for AssemblyName (#82754)
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n authored Feb 28, 2023
1 parent a3290ee commit 5c9c120
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestType_IHelloService, InternalInvokeProxy>();
proxy.Hello("Hello");
return true;
}

private static TInterface CreateHelper<TInterface, TProxy>(bool useGenericCreate) where TProxy : DispatchProxy
{
if (useGenericCreate)
Expand Down

0 comments on commit 5c9c120

Please sign in to comment.