C#接口(不同项目/程序集中的调用方法) [英] C# Interface (Call Method in Different Project/Assembly)

查看:529
本文介绍了C#接口(不同项目/程序集中的调用方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码几乎总结了我想要实现的目标。

The code below pretty much sums up what I want to achieve.

我们有一个包含许多不同项目的解决方案,但是我们需要能够从未引用的项目中调用项目中的方法(会导致循环引用)。

We have a solution which comprises many different projects however we have a need to be able to call methods in projects from projects which are not referenced (would cause circular reference).

我已经发布了以前的问题,下面的代码几乎就是我用接口提出的。我仍然不知道如何调用驻留在未引用的其他项目中的方法。

I have posted previous questions and the code below is pretty much what I have come up with using interfaces. I still do not know how I can call a method that resides in a different project that is not referenced.

我无法创建接口的实例,它必须是一类。但是,如何创建未引用的类的实例。我不想为此使用反射。

I cannot create an instance of the interface, it has to be a class. But how can I create an instance of a class that is not referenced. I do not want to use reflection for this.

代码是C#2.0

感谢任何帮助。

我需要在GeneralMethod(Class Raise)中放置哪些代码才能在Listen类中执行Update方法?

What code do I need to place in "GeneralMethod" (Class Raise) to be able to execute the "Update" method in Class "Listen" ?

// Link Project
namespace Stack.Link
{
    public class Interface
    {
        public interface Update
        {
            void Update();
        }
    }
}

// Project A
// References Link only
namespace Stack.ProjA
{
    public class Raise
    {
        public void GeneralMethod()
        {
            // I want to place code in here to be able to execute 
            // "Update" method in ProjB.
            // Keep in mind that ProjA and ProjB only reference
            // Link Project            
        }
    }
}

// Project B
// References Link only
namespace Stack.ProjB
{
    public class Listen : Stack.Link.Interface.Update
    {
        public void Update()
        {
            // Do something here that is executed from ProjA
            Console.Write("Executed Method in ProjB");
        }
    }
}

我应该澄清动机需要这样做的背后。也许有更好的方法....

I should probably clarify the motivation behind needing to do this. Perhaps there is a better way ....

我们有一个基础形式,所有其他项目都是从中引用的。作为一个例子,我们将一个包含各种设置的对象传递给项目(来自基础形式)。

We have a baseform from which all other projects are referenced. As an example we pass an object which contains various settings to the project when it is loaded (from the baseform).

例如,如果设置对象有一些变量更改(在baseform中填充设置对象),我们希望加载的项目监听此更改并获取新的设置对象。

If for example, the settings object has some variables change (settings object populated in baseform), we would like the loaded project to listen for this change and obtain a new settings object.

因为baseform引用了所有其他项目,我们需要让项目监听基础形式中的事件。

Because the baseform references all the other projects, we need to have the projects "listen" for events in the baseform.

清除泥浆: - )

推荐答案

我使用 Activator.CreateInstance 来执行此操作。您从路径加载程序集,然后创建该类的实例。例如,您可以使用此方法在主机应用程序中加载小工具程序集,主机在编译时不知道小工具。

I used Activator.CreateInstance to do this. You load the assembly from a path and then create an instance of the class. For example, you could use this to load gadgets assemblies in a host application where the host doesnt know about the gadgets at compile time.

示例伪代码(无错误处理)

Sample Pseudo Code (no error handling)

为要加载的类创建一个接口:

Create an interface for the class you are going to load:

public interface IRemote
{
     void Update();
}

然后你需要一个加载程序集并调用函数的方法

Then you need a method to load the Assembly and call the function

现在这里有一个使用这一切的方法:

Now here is a method to use all this:

private void
   DoRemoteUpdate( string assemblyPath, string className )
{ 
   Assembly assembly   = Assembly.Load(assemblyPath); 
   Type     objectType = assembly.GetType(className); 

   remoteAssembly = (IRemote)Activator.CreateInstance(objectType); 
   remoteAssembly.Update(); 
}

这篇关于C#接口(不同项目/程序集中的调用方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆