I'm writing a program which will allow to load a specific managed .DLL file and play with it. Since I want the ability to unload the .DLL file, I'm creating two AppDomains - one for the app itself, the other for the currently loaded .DLL.
Since most of the objects in the loaded .DLL do not serialize well, I'm creating a MarshalByRefObject
wrapper class which will keep the object itself in its own AppDomain, and expose some reflection functions to the main application AppDomain.
However when I try to invoke a method on the remote object I get stuck with an exception:
Permission denied: cannot call non-public or static methods remotely.
This is very strange, because I'm not using any non-public or static methods at all. In essence, what I have is:
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 the exception when I try to do:
var c = SomeInstanceOfRemoteObjectWrapper.InvokeMethod<RemoteObjectWrapper>("somePublicMethod", "some string parameter");
What's going on here? As far as I can understand, the InvokeMethod
method doesn't even get executed, the exception is thrown when I try to run it.
Added: To clarify - SomeInstanceOfRemoteObjectWrapper
is constructed in the .DLL's AppDomain and then returned to my main AppDomain, The InvokeMethod<T>()
is called from my main AppDomain (and I expect it to execute in the .DLL's AppDomain).
Even the method InvokeMethod<T>
is public it is contained within an internal class. No modifier in C# makes it internal. Hence the effective visibility of InvokeMethod<T>
is internal and you are getting the exception. Making RemoteObjectWrapper
public should fix this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With