【发布时间】:2013-10-26 02:20:31
【问题描述】:
VS2012 for desktop .net framework 4.5 normal windows forms applications, not WPF
您好,我试图寻找答案,但我不确定正确的术语。我设法破坏了我的代码,但无法理解我做错了什么。 (我认为我没有改变任何东西,但是......)
我有一个包含 2 个项目的解决方案。第一个项目是可执行程序,第二个是DLL,在运行时加载并供第一个项目使用。
第一个项目包含一个表单和一个在同一命名空间中具有公共静态字符串的静态类。 (以及其他一些未连接的类)。具体来说:
namespace project1_namespace
{
static class settings
{
public static string some_words = "some words in a string";
}
class dll_callback{
//.. some public methods here
}
dll_callback dllcallback; // instance is initialised in the code (not shown)
Form form;
public partial class frm_splash : Form
{
private void frm_splash_FormClosing(object sender, FormClosingEventArgs e)
{
// this function actually loads the DLL, ensuring its the last step
//... some error checking code removed for brevity
Assembly assembly = Assembly.LoadFrom("c:\dllpath\project2.dll");
Type type_init = assembly.GetType("project2_class");
object init = Activator.CreateInstance(type_init, form, dllcallback);
//... some error checking code removed for brevity
}// end method
}// end form class
}// end namespace
当窗体关闭时,调用上面显示的方法,该方法调用第二个项目类 project2_class 构造函数。
在项目2的DLL中,有:
namespace project2_namespace
{
// how did i get this working to reference "settings" class from project 1??
public class project2_class
{
public project2_class(project2_namespace.Form1 form_ref, object callback)
{
settings.some_words = "the words have changed";
//... some more stuff
}
}
}
现在,我在 project2 的一个完全不同的部分试验了一些代码,VS2012 突然开始拒绝编译:
错误 CS0103:当前上下文中不存在名称“设置”
对此的标准解决方案似乎是添加对 project2 的引用,但这会创建循环依赖关系,因为项目 1 将 2 作为 DLL 调用。
老实说,我真的不认为我改变了与此相关的任何内容,但显然我已经改变了。 看着它,我看不到项目 2 如何在没有引用的情况下访问项目 1 中的类,但是 project2_class 构造函数的参数列表不包括一个,我绝对肯定它没有改变(而且出于向后兼容性的原因,我无法更改它)。 非常感谢您对此的帮助,因为要完成这项工作需要做很多工作。
附带说明一下,我确实吸取了关于不使用源代码控制的教训。而不是制作“这是如何工作的”cmets 而不是“这是做什么的”cmets。
【问题讨论】:
标签: c# .net visual-studio-2012 dll .net-assembly