首页 > 解决方案 > 是否可以在不参考其所在项目的情况下调用方法?

问题描述

我有两个项目:引擎,客户端。在执行 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){
            ...
         }
      }
   }
}

标签: c#

解决方案


您可以使用反射和类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());
        }
    }
}

推荐阅读