Skip to content

System.Reflection.DispatchProxy inconsistently fails for internal interfaces #30917

Description

@AArnott

An undocumented limitation in DispatchProxy.Create is that it sometimes throws for various configurations of visibility. This seems to be a broader issue than the more specific #25595.

These configurations work:

  1. public interface, public base class.
  2. internal interface, internal base class, within the same assembly. But only when this is the first proxy ever generated.

These configurations don't work:

  1. public interface, internal base class.
  2. internal interface, internal base class, if another assembly has already provided an internal interface to generate a proxy for.

There were other odd configurations that similarly failed.

Since I've done this kind of feature before, I have a suspicion that the generated ProxyBuilder assembly includes an System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute for the assembly that defines the interface.
The way I solved this is that each time I need to generate a proxy, I evaluate all the assemblies that visibility checks must be skipped for and create a new dynamic assembly for each unique set that I get. So multiple proxies are supported, and we reuse dynamic assemblies where possible but when the set of dynamic assemblies exceed any previously discovered set we have to start a new assembly.

Adding more attributes to an existing dynamic assembly is ineffective because once you've loaded the first type from the assembly, the CLR caches these attributes and doesn't notice when new ones are added.

Metadata

Metadata

Assignees

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions