You can do this using reflection and class System.Activator
("mscorlib.dll" assembly). Define your classes as below:
In project Engine
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 });
}
}
}
In project Client,
class ClientAction
namespace Client
{
public class ClientAction
{
public ClientAction() { }
public void OpenForm(object obj1, object obj2)
{
new Form1()
{
Text = "OpenForm(object obj1, object obj2)"
}.Show();
}
}
}
Now in project Client, you can test it in class Program
like this:
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());
}
}
}
Another option is using delegates. You must create a static event in project Engine and in project Client subscribe a handler to this event which will invoke required method.
In project Engine
the delegate:
namespace Engine
{
public class OpenFormEventArgs
{
public object Obj1 { get; set; }
public object Obj2 { get; set; }
}
public delegate void OpenFormEventHandler(object sender, OpenFormEventArgs e);
}
class 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);
}
}
}
In project Client,
class 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();
}
}
}
Now in project Client, you can test it in class Program
like this:
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());
}
}
}