I have dlls sum and interface
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 loaded like this:
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.
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.