.NET exception remoting: Permission denied: cannot remove non-public or static methods remotely

I am writing a program that will allow you to download a specific .DLL file and play with it. Since I want the ability to upload a .DLL file, I create two AppDomains - one for the application itself and one for the currently downloaded .DLL.

Since most of the objects in the loaded .DLL do not serialize well, I create a wrapper class MarshalByRefObjectthat will save the object itself in its own AppDomain and output some reflection functions to the main AppDomain application.

However, when I try to call a method on a remote object, I get stuck with the exception:

Permission denied: cannot remotely call non-public or static methods.

This is very strange because I do not use any non-public or static methods at all. Essentially, I have:

class RemoteObjectWrapper: MarshalByRefObject
{
    private Type SourceType;
    private object Source;

    public RemoteObjectWrapper(object source)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        this.Source = source;
        this.SourceType = source.GetType();
    }
    public T WrapValue<T>(object value)
    {
        if ( value == null )
            return default(T);
        var TType = typeof(T);
        if (TType == typeof(RemoteObjectWrapper))
            value = new RemoteObjectWrapper(value);
        return (T)value;
    }
    public T InvokeMethod<T>(string methodName, params object[] args)
    {
        return WrapValue<T>(SourceType.InvokeMember(methodName,
            System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance |
            System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public, null, this.Source, args));

    }
}

And I get an exception when I try to do:

var c = SomeInstanceOfRemoteObjectWrapper.InvokeMethod<RemoteObjectWrapper>("somePublicMethod", "some string parameter");

What's going on here? As far as I understand, the method InvokeMethodis not even executed, an exception occurs when I try to run it.

Added: To clarify - SomeInstanceOfRemoteObjectWrapperit is created in .DLL AppDomain and then returned to my main AppDomain, it InvokeMethod<T>()is called from my main AppDomain (and I expect it to be executed in .DLL AppDomain).

+3
source share
2 answers

InvokeMethod<T> , . # . , InvokeMethod<T> , . RemoteObjectWrapper public .

+5

. , :

class MyClass : MarshalByRefObject
{
  public T Foo<T>() where T : MarshalByRefObject {
    // stuff
  }
}

MyClass.Foo T , , MyClass . , , , , , - T. , , , . , Foo:

class MyClass : MarshalByRefObject
{
  public object Foo(Type t) {
    // stuff
  }
}

. , , , .

+2

Source: https://habr.com/ru/post/1738126/


All Articles