简体   繁体   English

在C#中与DLL共享静态类而不传递引用

[英]sharing a static class with a DLL in C# without passing a reference

VS2012 for desktop .net framework 4.5 normal windows forms applications, not WPF VS2012 for Desktop .net Framework 4.5普通Windows窗体应用程序,而不是WPF
Hello, I tried to search for an answer, but I'm not sure of the correct terminology. 您好,我试图寻找答案,但是我不确定正确的术语。 I've managed to break my code, and can't understand what I've done wrong. 我设法破坏了代码,不明白自己做错了什么。 (i didn't think i had changed anything, but ...) (我认为我没有做任何更改,但是...)

I have a solution which contains 2 projects. 我有一个包含2个项目的解决方案。 The first project is an executable program, and the second is a DLL, which is loaded at run time and used by the first project. 第一个项目是可执行程序,第二个项目是DLL,DLL在运行时加载并由第一个项目使用。

the first project contains a form, and a static class with public static strings in the same namespace. 第一个项目包含一个表单,以及一个在同一名称空间中带有公共静态字符串的静态类。 (and some other unconnected classes). (以及其他一些未连接的类)。 specifically: 特别:

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

when the form is closing, the method shown above is called which calls the second projects class project2_class constructor. 当窗体关闭时,将调用上面显示的方法,该方法将调用第二个项目类project2_class构造函数。

in project 2, the DLL, there is: 在项目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
        }
    }
}

Now, i was experimenting with some code in an entirely different part of project2, and VS2012 suddenly started refusing to compile stating: 现在,我正在project2的完全不同的部分中尝试一些代码,而VS2012突然开始拒绝编译说明:
error CS0103: The name 'settings' does not exist in the current context 错误CS0103:名称“设置”在当前上下文中不存在

the standard solution to this appears to be to add a reference to project2, but that would create circular dependencies because project 1 calls 2 as a DLL. 对此的标准解决方案似乎是添加对project2的引用,但这将创建循环依赖项,因为项目1调用2作为DLL。

I really honestly don't think i had changed anything relevant to this, but also clearly I have. 老实说,我真的没有改变任何与此相关的内容,但显然我也做了。 looking at it, i cant see how project 2 would have access to a class in project 1 without a reference, but the list of arguments to the project2_class constructor doesn't include one, and I am absolutely positive that it hasn't changed (and I cant change it for backwards compatibility reasons). 看着它,我看不到项目2如何在没有引用的情况下访问项目1中的类,但是project2_class构造函数的参数列表不包含一个,并且我绝对肯定它没有改变(而且由于向后兼容的原因,我无法更改它)。 would really appreciate help with this, as its been a lot of work to get this working. 非常感谢您提供帮助,因为要使其正常工作需要大量的工作。


as a side note, I've definitely learned my lesson about not using source control. 作为附带说明,我肯定已经学到了关于不使用源代码控制的课程。 and not making "how this works" comments instead of "what this does" comments. 而不是发表“这是如何工作的”评论,而不是发表“这是做什么的”评论。

may dynamic help you? 动态可以帮助您吗? You can not get the setting string at complie time. 您无法在编译时获取设置字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM