ObjectHandle unwrap给我一个MarshalbyRefObject对象而不是我的类对象

时间:2015-07-01 21:38:24

标签: c# .net-assembly

我正在尝试将程序集加载到Appdomain中,并且我的ObjectHandle似乎遇到了一些问题。

程序集A只包含一个类:

namespace MyTestAssembly
{
    using System; 

    public class SomeClass : MarshalByRefObject
    {
        public virtual bool Run()
        {
            return true; 
        }
}

在程序集B中,我尝试像这样调用类:

        var assembly_path = "/path/to/something.dll"
        System.AppDomain appDomain = System.AppDomain.CreateDomain("MyDomain");
        System.Runtime.Remoting.ObjectHandle oh = appDomain.CreateInstanceFrom(assembly_path, "MyTestAssembly.SomeClass");
        object obj = oh.Unwrap();
        obj.GetType().InvokeMember("Run", 
            System.Reflection.BindingFlags.InvokeMethod,
            Type.DefaultBinder, obj, null);
        AppDomain.Unload(appDomain);

这给了我例外:

An exception of type 'System.MissingMethodException' occurred in    

mscorlib.dll but was not handled in user code

Additional information: Attempted to access a missing member.

如果我在期望它成为“SomeClass”对象时检查obj的类型是MarshalByRefObject。我在这里错过了什么吗?我试着看一些例子,看起来大多数人都是这样做的。

0 个答案:

没有答案