简体   繁体   English

.NET远程处理异常:权限被拒绝:无法远程调用非公共或静态方法

[英].NET remoting exception: Permission denied: cannot call non-public or static methods remotely

I'm writing a program which will allow to load a specific managed .DLL file and play with it. 我正在编写一个程序,它允许加载特定的托管.DLL文件并使用它。 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文件,我正在创建两个AppDomain - 一个用于app本身,另一个用于当前加载的.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. 由于加载的.DLL中的大多数对象都没有很好地序列化,我正在创建一个MarshalByRefObject包装类,它将对象本身保存在自己的AppDomain中,并将一些反射函数暴露给主应用程序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. 据我所知, InvokeMethod方法甚至没有执行,当我尝试运行它时抛出异常。

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). 补充:为了澄清 - SomeInstanceOfRemoteObjectWrapper在.DLL的AppDomain中构建,然后返回到我的主AppDomain, InvokeMethod<T>()从我的主AppDomain调用(我希望它在.DLL的AppDomain中执行)。

Even the method InvokeMethod<T> is public it is contained within an internal class. 即使InvokeMethod<T>方法是公共的,它也包含在内部类中。 No modifier in C# makes it internal. C#中没有修饰符使其成为内部修饰符。 Hence the effective visibility of InvokeMethod<T> is internal and you are getting the exception. 因此, InvokeMethod<T>的有效可见性是内部的,您将获得异常。 Making RemoteObjectWrapper public should fix this problem. 使RemoteObjectWrapper公开应解决此问题。

I have the exact same problem. 我有完全一样的问题。 If you an object such as: 如果您是一个对象,例如:

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

Calls to MyClass.Foo with an internal type T work fine but only as long as your MyClass object is not remote. 使用内部类型T调用MyClass.Foo可以正常工作,但只要您的MyClass对象不是远程的。 I suppose this is because you can't necessarily assume the place you're making the call to has any access to T. It's very awkward, however, when you can't make the type you're using public. 我想这是因为你不一定假设你正在进行呼叫的地方有任何访问权限。但是,当你无法公开你使用的类型时,这是非常尴尬的。 You can sometimes get around it by wrapping or replacing Foo with: 你有时可以通过包装或替换Foo来解决它:

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

Which isn't type safe at all and requires a cast on the caller's side. 这根本不是类型安全的,需要在呼叫者方面进行投射。 I don't know if there's a better solution, though, if you can't be bothered to expose your internal type. 但是,我不知道是否有更好的解决方案,如果你不能打扰你的内部类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mocking .NET class的“非公会员” - Mocking "non-public members" of a .NET class Protobuf.NET是否处理[OnSerializing]? “非公共成员不能与完整的dll编译一起使用” - Does Protobuf.NET process [OnSerializing]? “Non-public member cannot be used with full dll compilation” 在dotnet核心控制器中进行单元测试非公开方法 - Unit testing non-public methods in dotnet core controller 为什么不能隐式实现非公共接口成员? - Why cannot implicitly implement a non-public interface member? .NET:从动态程序集中访问非公共成员 - .NET: Accessing non-public members from a dynamic assembly 权限被拒绝-使用.NET Remoting和FrameworkElementAdapters的跨进程UI - Permission Denied - Cross process UI using .NET Remoting and FrameworkElementAdapters .NET 4.x 抱怨非公共属性的反序列化,而不是 .NET 3.5 - 如何解决? - .NET 4.x complains about deserialization of non-public properties as opposed to .NET 3.5 - how to fix it? SqlCommand 和 ExecuteReader 返回非公共成员 - SqlCommand and ExecuteReader return Non-Public Members C#CreateDocumentType用于非PUBLIC - C# CreateDocumentType for non-PUBLIC NHibernate可以设置非公共属性吗? - Can NHibernate set non-public properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM