.NET远程异常:权限被拒绝:无法远程调用非公共或静态方法 [英] .NET remoting exception: Permission denied: cannot call non-public or static methods remotely
问题描述
我正在写一个程序,这将使装载特定的管理.dll文件并使用它。因为我要卸载该.dll文件的能力,我创建了两个应用程序域 - 一个用于应用程序本身,另一个用于当前加载.DLL
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.
由于大部分在加载.DLL对象不序列化好,我创建一个 MarshalByRefObject的
包装类,这将使物体本身在其自己的AppDomain,并揭露一些反射功能的主要应用程序的AppDomain。
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");
这是怎么回事呢?据我可以理解,在的InvokeMethod
方法甚至没有得到执行,当我尝试运行它的异常。
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.
补充:为了澄清 - SomeInstanceOfRemoteObjectWrapper
是建于.DLL的AppDomain中,然后回到我的主要的AppDomain中,的InvokeMethod&LT; T&GT;()
是我的主要的AppDomain叫(我期望它在.DLL的AppDomain中执行)
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).
推荐答案
连方法的InvokeMethod&LT; T&GT;
是公开的,并且包含一个内部类中。在C#中没有修改使其内部。因此,的InvokeMethod&LT的有效可视性; T&GT;
是内部的,你所得到的例外。制作 RemoteObjectWrapper
公众应该解决这个问题。
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.
这篇关于.NET远程异常:权限被拒绝:无法远程调用非公共或静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!