简体   繁体   English

C#事件Dll接口

[英]C# Events Dll Interface

I have dlls sum and interface 我有dll的总和和接口

public interface IPlugin
{
    string Name {get; }
    string GetDescription();

    double GetLastResult { get; }
    double Execute(double value1, double value2);

    event EventHandler OnExecute;

    void ExceptionTest(string input);
}

public class Sum : IPlugin
{      
    public string Name
    {
        get { return "Sum"; }
    }

    public event EventHandler OnExecute;
}

How to make event like I take name of dll,event say that name was taken? 如何使事件像我以dll的名字命名,事件说该名字已被使用?

DLL loaded like this: DLL加载如下:

private PluginInterface.IPlugin LoadAssembly(string assemblyPath)
{
    string assembly = Path.GetFullPath(assemblyPath);
    Assembly ptrAssembly = Assembly.LoadFile(assembly);

    foreach (Type item in ptrAssembly.GetTypes())
    {
        if (!item.IsClass) continue;
        if (item.GetInterfaces().Contains(typeof(PluginInterface.IPlugin)))
        {
            return (PluginInterface.IPlugin)Activator.CreateInstance(item);
        }
    }
    throw new Exception("Invalid DLL, Interface not found!");
}

IPlugin currPlugin;
currPlugin = LoadAssembly("pathtodll");
MessageBox.Show(currPlugin.Name);

I tried to made event like this: 我试图做这样的事件:

currPlugin.OnExecute += currPlugin_OnExecute; //subscribe to dll
void currPlugin_OnExecute(object sender, EventArgs e)
{
    MessageBox.Show("Event OnExecute triggered from DLL " + ((PluginInterface.IPlugin)sender).Name);
}

But i don't know what to do next Please,help... 但我不知道下一步该怎么做,请帮助...

You need raise the event OnExecute inside double Execute(double value1, double value2) implementation. 您需要在double Execute(double value1, double value2)实现内引发事件OnExecute

This example is just for test. 此示例仅用于测试。

internal class Program
{
    private static void Main(string[] args)
    {
        //load the .exe assembly using reflection(just for testing)
        var currPlugin = LoadAssembly(Assembly.GetEntryAssembly().Location);
        Console.WriteLine(currPlugin.Name);
        currPlugin.OnExecute += currPlugin_OnExecute;
        currPlugin.Execute(10d, 20d);
    }

    private static void currPlugin_OnExecute(object sender, EventArgs e)
    {
        var plugin = (IPlugin) sender;

        Console.WriteLine("Event OnExecute triggered from DLL '{0}'", plugin.GetType().Assembly.FullName);
        Console.WriteLine("[{0}] Event OnExecute -> {1}", plugin.Name, plugin.GetLastResult);
    }

    private static IPlugin LoadAssembly(string assemblyPath)
    {
        string assembly = Path.GetFullPath(assemblyPath);
        Assembly ptrAssembly = Assembly.LoadFile(assembly);

        foreach (Type item in ptrAssembly.GetTypes())
        {
            if (!item.IsClass) continue;
            if (item.GetInterfaces().Contains(typeof (IPlugin)))
            {
                return (IPlugin) Activator.CreateInstance(item);
            }
        }
        throw new Exception("Invalid DLL, Interface not found!");
    }
}

public interface IPlugin
{
    string Name { get; }
    string GetDescription();

    double GetLastResult { get; }
    double Execute(double value1, double value2);

    event EventHandler OnExecute;

    void ExceptionTest(string input);
}

public class Sum : IPlugin
{
    private double _lastResult = double.NaN;

    public string Name
    {
        get { return "Sum"; }
    }

    public string GetDescription()
    {
        return "Plugin description";
    }

    public double GetLastResult
    {
        get { return _lastResult; }
    }

    public double Execute(double value1, double value2)
    {
        _lastResult = value1 + value2;
        OnExecuteInvoke();
        return _lastResult;
    }

    public event EventHandler OnExecute;

    protected virtual void OnExecuteInvoke()
    {
        var handler = OnExecute;
        if (handler != null)
            handler(this, EventArgs.Empty);
    }

    public void ExceptionTest(string input)
    {
        throw new Exception(input);
    }
}

public interface IPlugin { void Execute(); event EventHandler OnExecute; } public class Sum : IPlugin { public void Execute() { if (this.OnExecute != null) this.OnExecute(this, new EventArgs()); } public event EventHandler OnExecute; } ////////// use Sum sum = new Sum(); Console.WriteLine("Before subscribe"); sum.Execute(); sum.OnExecute += new EventHandler(sum_OnExecute); Console.WriteLine("After subscribe"); sum.Execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM