我有一类别 Project_Dll ,
首先在一开始 Form_Message() new 出 project public Form_Message()
{
InitializeComponent(); Projrct_Dll project = new Projrct_Dll();
project.dll_load();
} 接着我在另一Form_Message_Load想继续使用 project 该怎么引用此 project 呢? private void Form_Message_Load(object sender, EventArgs e)
{
MethodInfo mi = project.dll_method("show");
} class Projrct_Dll
{
internal Assembly dllFile = null;
internal Type type = null;
internal Object obj = null; public void dll_load()
{
dllFile = System.Reflection.Assembly.LoadFrom("test_lib");
type = dllFile.GetType("test_lib.Message_Show");
obj = dllFile.CreateInstance(type.FullName, true);
}
public MethodInfo dll_method(string method_name)
{
MethodInfo mi = type.GetMethod(method_name);
return mi;
}
}
public Projrct_Dll project = new Projrct_Dll();
public static Projrct_Dll project = new Projrct_Dll();// 或者
class Projrct_Dll
{
public static MethodInfo GetMethod(string methodName)
{
Assembly assembly = Assembly.LoadFrom("test_lib");
Type type = assembly.GetType("test_lib.Message_Show");
return (type.GetMethod(methodName));
}
}
感谢1楼的提示
的确是要使用单件模式
设为 private static
并在自订类别里新增
public static Projrct_Dll get_instance()
{
if (instance == null)
instance = new Projrct_Dll(); return instance;
}
这样在各类别里就可去得到 project 并去选择执行哪种 method