Skip to content
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

DispatchProxy does not allow for building collectible assemblies. #30032

Closed
cablooshe opened this issue Jun 25, 2019 · 1 comment
Closed

DispatchProxy does not allow for building collectible assemblies. #30032

cablooshe opened this issue Jun 25, 2019 · 1 comment
Labels
area-System.Reflection enhancement Product code improvement that does NOT require public API changes/additions
Milestone

Comments

@cablooshe
Copy link

Currently when you create a DispatchProxy, it makes the DynamicAssembly that generates the IL an noncollectable assembly (it's hardcoded to use AssemblyBuildAccess.Run here). This makes it impossible to create DispatchProxies inside collectible AssemblyLoadContexts.

If you try and use this create within a collectible AssemblyLoadContext, it fails with System.NotSupportedException: 'A non-collectible assembly may not reference a collectible assembly.'

public class ExampleDispatch<I> : DispatchProxy
{
    public object instance;
    internal static I Create<Dispatch>(Type type)
    {
        object proxy = Create<I, ExampleDispatch<I>>(); //Error throws here
        ((ExampleDispatch<I>)proxy).SetParameters(type, new Type[] { }, new object[] { });
        return (I)proxy;
    }

    private void SetParameters(Type instanceType, Type[] argTypes, object[] constructorArgs)
    {
        instance = instanceType.GetConstructor(argTypes).Invoke(constructorArgs);
    }

    protected override object Invoke(MethodInfo targetMethod, object[] args)
    {
        return targetMethod.Invoke(instance, args);
    }
}

A few options to fix this could be to change DispatchProxy's dynamic assembly to run with AssemblyBuildAccess.RunAndCollect, or allow for manually entering an option in DispatchProxy.Create<T, TProxy>() to decide if it should be collectible or not.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the Future milestone Feb 1, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@steveharter
Copy link
Member

steveharter commented Mar 19, 2020

or allow for manually entering an option in DispatchProxy.Create<T, TProxy>() to decide if it should be collectible or not.

This seems to be a viable option since it won't break existing users.

However, DispatchProxy investment is not our roadmap for 5.0. If there are additional demand for this, we can reconsider.

cc @GrabYourPitchforks

@steveharter steveharter removed the untriaged New issue has not been triaged by the area owner label Nov 12, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection enhancement Product code improvement that does NOT require public API changes/additions
Projects
None yet
Development

No branches or pull requests

4 participants