c# - 是否可以在不参考其所在项目的情况下调用方法?
问题描述
我有两个项目:引擎,客户端。在执行 Engine1.cs 类期间,我想通过从 Engine.cs 传递对象来从 ClientAction 类打开一个 Windows 窗体。客户引用引擎项目。
namespace Engine {
public Class Engine1 {
public Engine1() {
}
//what I would do if I could reference the Client project
ClientAction.OpenForm(obj1, obj2);
}
}
using Engine;
namespace Client {
public Class ClientAction {
public ClientAction() { }
public OpenForm(object obj1, object obj2) {
Form1.Open(obj1, obj2){
...
}
}
}
}
解决方案
您可以使用反射和类System.Activator
(“mscorlib.dll”程序集)来做到这一点。如下定义您的类:
在项目引擎中
using System;
using System.Reflection;
namespace Engine
{
public class Engine1
{
public Engine1()
{
var clientAction = Activator.CreateInstance(
Type.GetType("Client.ClientAction, Client"), new object[] { });
MethodInfo methodInfo = clientAction.GetType().GetMethod("OpenForm");
var arg1 = new object();
var arg2 = new object();
methodInfo.Invoke(clientAction, new object[] { arg1, arg2 });
}
}
}
在项目Client中,类ClientAction
namespace Client
{
public class ClientAction
{
public ClientAction() { }
public void OpenForm(object obj1, object obj2)
{
new Form1()
{
Text = "OpenForm(object obj1, object obj2)"
}.Show();
}
}
}
现在在项目Client中,您可以像这样在类中测试它Program
:
using System;
using System.Windows.Forms;
namespace Client
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var engine1 = new Engine.Engine1();
Application.Run(new Form1());
}
}
}
另一种选择是使用 delegates。您必须在项目引擎中创建一个静态事件,并在项目客户端中为该事件订阅一个处理程序,该事件将调用所需的方法。
在项目引擎中
代表:
namespace Engine
{
public class OpenFormEventArgs
{
public object Obj1 { get; set; }
public object Obj2 { get; set; }
}
public delegate void OpenFormEventHandler(object sender, OpenFormEventArgs e);
}
班级Engine1
namespace Engine
{
public class Engine1
{
public static event OpenFormEventHandler OpenForm;
public Engine1()
{
var obj1 = new object();
var obj2 = new object();
OpenFormEventArgs e = new OpenFormEventArgs() { Obj1 = obj1, Obj2 = obj2 };
OpenForm?.Invoke(this, e);
}
}
}
在项目Client中,类ClientAction
namespace Client
{
public class ClientAction
{
public ClientAction()
{
Engine.Engine1.OpenForm += Engine1_OpenForm;
}
private void Engine1_OpenForm(object sender, Engine.OpenFormEventArgs e)
{
OpenForm(e.Obj1, e.Obj2);
}
public void OpenForm(object obj1, object obj2)
{
new Form1()
{
Text = "OpenForm(object obj1, object obj2)"
}.Show();
}
}
}
现在在项目Client中,您可以像这样在类中测试它Program
:
using System;
using System.Windows.Forms;
namespace Client
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var clientAction = new ClientAction();
var engine1 = new Engine.Engine1();
Application.Run(new Form1());
}
}
}
推荐阅读
- css - 使用 animate.css 关闭引导模式
- php - 如何修复代码的第二部分?
- javascript - 我有 contenteditable div ,它里面是一个不可编辑的跨度,如何能够通过跨度文本长按来标记 div 的文本
- parsing - 解析 PDF 数据时如何考虑语法空白?
- android - SSD 边界框矩形缩放
- routes - BottomNavigationBar 保存偏移位置并导航到细节颤动
- eclipse - Eclipse 2018-12 是否适用于 32 位 Windows 或 Linux?
- django - 在 Django 中测试全文搜索的触发器
- java - 收到“javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted”这个错误
- java - BeanDefinitionStoreException:IOException 从类路径资源 [applicationContext] 解析 XML 文档