我有一个应用程序,它由一个 Windows 窗体项目和一个类库项目组成。当它启动可执行文件时,它会在 dll 中设置一个静态值。
using MyClassLibrary;
namespace MyExeApplication
{
public partial class MainForm : Form
{
Hashtable ht = null;
void Form_Load(...)
{
ht = new Hashtable();
ht.add("1", "Open");
ht.add("2", "Close");
Calculate.BasicValues = ht;
}
public Hashtable GetBasicValues()
{
return ht;
}
}
}
namespace MyClassLibrary
{
public class Calculate()
{
public static Hashtable BasicValues {get; set;}
}
}
现在假设应用程序正在内存中运行(可执行文件)。我的目的是创建另一个独立的应用程序并使用类库中函数 Calculate 中的值 BasicValues。
using MyClassLibrary;
namespace TestApplication
{
public partial class MainForm : Form
{
private void TestValueFromDll()
{
System.Windows.Forms.MessageBox.Show("Values of Hashtable");
Hashtable ht = Calculate.BasicValues;
//The hashtable is null and values are not there
//The above will not work. Could I say something like
//Get the running instance of MyExeApplication
//and invoke GetBasicValues() ?
}
}
}
我猜它不起作用,因为我的 TestApplication 已将 MyClassLibrary.dll 复制到可执行文件 TestApplication.exe 所在的 bin 文件夹中。因此使用了不同的 dll(不是第一个应用程序 MyExeApplication 使用的 dll)。
我的问题是如何解决这个问题?是否可以使用反射并获取 MyExeApplication 的实例并从那里读取值?还有其他方法吗?
谢谢