【发布时间】:2010-11-29 17:02:34
【问题描述】:
基本上问题是我想在未引用的程序集中调用一个方法,但似乎无法找到实例化类的正确调用。我尝试过简单的 Type t = Type.GetType("MyApp.Helper") 返回 null 和 Assembly.LoadFrom("MyApp.Helper") 引发安全异常。
在下面的示例中,两个项目/程序集(Helper.dll 和 Menu.dll)被分别编译到一个公共的“libs”文件夹中,但不相互引用。 Main.dll 引用两者,并且引用在 VS 中设置为“复制本地”。因此,当应用程序运行时,Main.xap 应该包含所有三个程序集,并且它们应该被加载到同一个应用程序域中。或者我的理解是这样。这是不可能的任务吗?我看到很多关于插件的 cmets,但到目前为止我还没有看到这种特定设计的示例。例如,我想我可以像 Jeff Prosise 描述的 here 那样做一些事情,但我宁愿将所有东西都放在一个包中。
这是我的代码草图:
在一个项目/装配中,我有一个工人阶级:
namespace MyApp.Helper {
public class Helper {
public void ShowHelp() {
Console.Write("Help!");
}
}
}
在另一个项目/程序集中,我有一个菜单类试图调用帮助器:
namespace MyApp.Menu {
public class Selector {
public void InvokeSelection(string className, string functionName) {
// fails: t will be null
Type t = Type.GetType(className);
// fails: t will be null
t = Type.GetType(string.Format("{0}.{1}, {0}, Version=1.0.0.0, Culture=\"\", PublicTokenKey=null", "MyApp.Helper", "Helper"));
// however, this works (reference to main assembly?)
t = Type.GetType(string.Format("{0}.{1}, {0}, Version=1.0.0.0, Culture=\"\", PublicTokenKey=null", "MyApp.Main", "Worker"));
// and, I'd like to do something like the following
// t.InvokeMember(functionName, ...);
}
}
}
最后,我有了主应用程序集:
namespace MyApp.Main {
public class Main {
public static void Main() {
MyApp.Menu.Selector sel = new Menu.Selector();
sel.InvokeSelection("MyApp.Help.Helper", "ShowHelp"); // fails
sel.InvokeSelection("MyApp.Main.Main", "Worker"); // works in some cases
}
public void Worker() {
Console.Write("Work!");
}
}
}
感谢您的任何想法!
-克里斯。
【问题讨论】:
标签: silverlight assemblies instantiation