我试图创建一个远程对象并获得错误,即使它看起来好像一切都井然有序,所以我决定直接从Microsoft(http://msdn.microsoft.com/)简化并运行一段代码示例EN-US /库/ 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,但这里是Stack:
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.
答案 0 :(得分:8)
在您的初始代码中,您尝试实例化一个不完整的类型名称,并且该代码不应该在任何地方工作,除非Worker没有名称空间。它应该是:
var workerType = typeof(Worker);
Worker remoteWorker = (Worker) ad.CreateInstanceAndUnwrap(
// Where Worker is defined (probably the same as current assembly in you case)
workerType.Assembly.FullName,
// Worker's *fully-qualified* typename, including the namespace
workerType.FullName);
您的本地激活码错误地调用typeof(Worker).GetType().FullName
而不是typeof(Worker).FullName
。
typeof(Anything).GetType()
将始终返回System.RuntimeType