Andrew Neillans <ne******@gmail.com> wrote:
Ok, hopefully this might help :)
Well, it doesn't particularly show where the problem is, unfortunately.
Usually plug-ins implement an interface defined by your main
application, and you use that to communicate. Is that a problem for you
in some way? If so, it would be helpful to know how, exactly. Note that
you could specify some kind of method in the interface which takes a
parameter which is another interface implemented this time by your main
application - for instance, an interface with DoLogging. You would then
be able to tell your plug-ins who they were registeredwith, and they
could use that to do logging.
Here's a short but complete example demonstrating that:
Interfaces.cs:
using System;
public interface ILogger
{
void Log (string x);
}
public interface IPlugin
{
ILogger Logger { set; }
void SayHello();
}
Test.cs:
using System;
using System.Reflection;
class Test
{
static void Main(string[] args)
{
Assembly a = Assembly.Load (args[0]);
ILogger log = new Logger();
foreach (Type t in a.GetTypes())
{
if (typeof(IPlugin).IsAssignableFrom(t))
{
IPlugin plugin = (IPlugin)
Activator.CreateInstance(t);
plugin.Logger = log;
plugin.SayHello();
}
}
}
}
class Logger : ILogger
{
public void Log (string x)
{
Console.WriteLine (x);
}
}
Plugin.cs:
using System;
public class Plugin : IPlugin
{
ILogger logger;
public ILogger Logger
{
set { logger = value; }
}
public void SayHello()
{
logger.Log ("Hello");
}
}
Compile:
csc /target:library Interfaces.dll
csc /r:Interfaces.dll Test.cs
csc /target:library /r:Interfaces.dll Plugin.cs
Run:
test plugin
Output:
Hello
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too