VS2012適用於桌面.net框架4.5正常的Windows窗體應用程序,而不是WPF
你好,我試圖尋找答案,但我不確定正確的術語。我設法打破了我的代碼,並且無法理解我做錯了什麼。 (我不認爲我改變了什麼,但是...)與C#中的DLL共享靜態類,無需傳遞參考
我有一個解決方案,其中包含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
}
}
}
現在,我是在項目2完全不同的部分有一些代碼嘗試和VS2012突然開始拒絕編譯指出:
錯誤CS0103 :當前上下文中不存在名稱'設置'
對此的標準解決方案似乎是向project2添加引用,但由於項目1將2調用爲DLL,因此會創建循環依賴項。
我真的很老實不認爲我改變了與此有關的任何事情,但也清楚我有。 看着它,我不能看到項目2如何在沒有引用的情況下訪問項目1中的類,但project2_class構造函數的參數列表不包含一個,我絕對肯定它沒有改變(並且因爲向後兼容性原因我不能改變它)。 將非常感謝這方面的幫助,因爲它已經做了很多工作來實現這一目標。
作爲一個便箋,我已經明確瞭解了關於不使用源碼控制的教訓。而不是「做這個工作」的意見,而不是「這是做什麼」的評論。
謝謝Marty,什麼是動態的?我知道我不喜歡那樣,但我一直在得到字符串。 (我有建立的程序和DLL來證明它) – user2921760