我试图创建一个远程对象并得到错误,即使看起来一切都井井有条,所以我决定简化并直接从 Microsoft 运行一段示例代码 (http://msdn.microsoft.com/en-us /library/system.marshalbyrefobject.aspx)。
当我运行此代码时,当它调用 CreateInstanceAndUnwrap() 时,我得到一个 TypeLoadException。但是,我只是在我的笔记本电脑上尝试过,它运行良好。任何人都知道为什么它可以在笔记本电脑上工作而不是台式机?我刚刚在每个上创建了一个控制台应用程序并复制/粘贴了下面的代码,所以我不明白两者之间有什么不同。
using System;
using System.Reflection;
public class Worker : MarshalByRefObject
{
public void PrintDomain()
{
Console.WriteLine("Object is executing in AppDomain \"{0}\"",
AppDomain.CurrentDomain.FriendlyName);
}
}
class Example
{
public static void Main()
{
// Create an ordinary instance in the current AppDomain
Worker localWorker = new Worker();
localWorker.PrintDomain();
// Create a new application domain, create an instance
// of Worker in the application domain, and execute code
// there.
AppDomain ad = AppDomain.CreateDomain("New domain");
Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
Assembly.GetExecutingAssembly().FullName,
"Worker");
remoteWorker.PrintDomain();
}
}
编辑:所以,我只是尝试在本地域中使用 Activator 创建类型的实例,甚至这给了我一个错误,说它无法加载文件或程序集“System.RuntimeType”或其依赖项之一。看到这是 .Net Framework 的内部类型,即使我愿意,我也无法阻止它部署,我现在很困惑。
编辑:没有 InnerException,但这里是堆栈:
at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)
at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(String assemblyName, String typeName)
at System.AppDomain.CreateInstance(String assemblyName, String typeName)
at System.AppDomain.CreateInstance(String assemblyName, String typeName)
at ConsoleApplication1.Example.Main(String[] args) in D:\Projects\Remoting\ConsoleApplication1\Program.cs:line 28
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
哦,例外情况如下:
{"Could not load type 'Worker' from assembly 'ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.":"Worker"}
另外,我尝试运行这行代码:
var w = Activator.CreateInstance(typeof(Worker).GetType().FullName, "Worker");
并得到一个 FileNotFoundException:
Could not load file or assembly 'System.RuntimeType' or one of its dependencies. The system cannot find the file specified.