Skip to content

Conversation

afifi-ins
Copy link

@afifi-ins afifi-ins commented Sep 26, 2025

This PR fixes two scenarios where DispatchProxy behaves incorrectly when working with multiple AssemblyLoadContexts (ALCs).
Fixes:

  1. Corrects proxy generation where the second ALC could produce a proxy bound to the wrong interface type, leading to InvalidCastException.
  2. Fixes the case of creating a proxy when the base proxyType is in a non-collectable ALC (in this case the default ALC) and the interface is in a collectable ALC.

Added Unit test to prevent regressions.

Fixes: #109398

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Sep 26, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 26, 2025
@afifi-ins afifi-ins force-pushed the DispatchProxyGeneraotor branch from f77dbf4 to eb9aeaf Compare September 26, 2025 20:56
@jkotas jkotas added area-System.Reflection and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Sep 26, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

Comment on lines +85 to +93
if (s_defaultProxyAssembly == null)
{
Interlocked.CompareExchange(ref s_defaultProxyAssembly, new ProxyAssembly(AssemblyLoadContext.Default), null);
}

ProxyAssembly proxyAssembly = s_alcProxyAssemblyMap.GetOrAdd(alc, static x => new ProxyAssembly(x));
GeneratedTypeInfo proxiedType = proxyAssembly.GetProxyType(baseType, interfaceType, interfaceParameter, proxyParameter);
return Activator.CreateInstance(proxiedType.GeneratedType, new object[] { proxiedType.MethodInfos })!;
proxyAssembly = s_defaultProxyAssembly;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benefit to special-case when both ALCs are the default one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Reflection community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DispatchProxy generates a proxy type implementing the interface from the wrong ALC
3 participants