2013-04-22 3 views
0

나는 플러그인 기반 응용 프로그램을 작성 중입니다.C# 플러그인과 호스트 응용 프로그램 간의 통신

호스트 응용 프로그램 :

namespace CSK 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 

    public MainWindow() 
    {     
     InitializeComponent(); 
     LoadPlugins(); 

    } 

    public void LoadPlugins() 
    { 
     DirectoryInfo di = new DirectoryInfo("./Plugins"); 

     foreach (FileInfo fi in di.GetFiles("*_Plugin.dll")) 
     { 
      Assembly pluginAssembly = Assembly.LoadFrom(fi.FullName); 

      foreach (Type pluginType in pluginAssembly.GetExportedTypes()) 
      { 

       if (pluginType.GetInterface(typeof(MainInterface.PluginHostInterface).Name) != null) 
       { 
        MainInterface.PluginHostInterface TypeLoadedFromPlugin = (MainInterface.PluginHostInterface)Activator.CreateInstance(pluginType); 
        MainInterface.IMother mother = new ApiMethods(this); 
        TypeLoadedFromPlugin.Initialize(mother); 
       } 

      } 
     } 
    } 
} 

인터페이스 :

namespace MainInterface 
{ 
public interface PluginHostInterface 
{ 
    void Initialize(IMother mother); 
} 

public interface IMother 
{ 
    MenuItem addMenuItem(String header, String name); 
    MenuItem addSubMenuItem(MenuItem menu, String header, String name); 
    Boolean receiveMessage(String message, String from); 
    Boolean addContact(String name, String status, String proto, String avatar = "av"); 
} 
} 

테스트 플러그인 :

namespace Plugin_Test 
{ 
public class MainClass : MainInterface.PluginHostInterface 
{ 
    private MainInterface.IMother CSK; 

    public void Initialize(MainInterface.IMother mainAppHandler) 
    { 
     CSK = mainAppHandler; 

    } 

} 
} 

그리고 지금, 난 내 호스트 응용 프로그램에서 플러그인 테스트에서 몇 가지 방법을 실행합니다. 물론 많은 플러그인이있을 것이며 모든 플러그인에 지정된 메소드가 포함되어있는 것은 아닙니다. 나는 이벤트를 사용하려고했지만 성공하지 못했습니다. 어떤 생각을하는 방법? 이벤트와

+0

TypeLoadedFromPlugin.Initialize (mother); <- you 여기에 호스트 응용 프로그램에서 플러그인의 기능을 실행하고 있습니다. 당신의 문제는 정확히 무엇입니까? – x4rf41

+0

예를 들어 마우스 클릭과 같은 플러그인에서 함수를 실행하고 싶습니다. 그리고 만약 내가 인터페이스 PluginHostInterface에서 새로운 메소드를 구현한다면 모든 플러그인에서 같은 메소드를 구현해야한다. – jankes83

+0

btw. 인터페이스에있는 모든 메소드를 구현하고 싶지 않으면 인터페이스 대신 추상 클래스를 사용하십시오. – x4rf41

답변

1

클래스 :

public class EventProvidingClass { 

    public event EventHandler SomeEvent; 

    public void InvokeSomeEvent() { 
     if(SomeEvent != null) SomeEvent.Invoke(this, new EventArgs()); 
    } 

} 

플러그인 인터페이스 :

public interface PluginHostInterface 
{ 
    void Initialize(IMother mother); 
    void InitializeEvents(EventProvidingClass eventProvider); 
} 

플러그인 클래스 : 다음

public class MainClass : MainInterface.PluginHostInterface 
{ 
    private MainInterface.IMother CSK; 

    public void Initialize(MainInterface.IMother mainAppHandler) 
    { 
     CSK = mainAppHandler; 
    } 

    public void InitializeEvents(EventProvidingClass eventProvider) 
    { 
     eventProvider.SomeEvent += someEventHandler; 
    } 

    private void someEventHandler(object sender, EventArgs e) 
    { 

    } 
} 

와는 Initialize 기능 후에 InitializeEvents를 호출합니다. 물론 이벤트를 원하는 위치에 넣을 수 있습니다. 플러그 인에서 EventHandler를 할당 할 수 있도록 플러그인을 사용할 수 있도록해야합니다.

관련 문제