473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Assembly Event Question

Hi all,

First off, I apologise if this is mentioned in a MSDN document
somewhere, but I've searched both MSDN and Google and can't find
anything - so thought I'd post.

Ok, now the problem.

I have an application which is dynamically loading a series of
Assemblies (as plugins to extend the main application). This works
wonderfully. However, I now need to provide a way for logging data back
from the assembly to the main application - like a CallBack in C.
However, I can not find anyway to do this -- lots of information on how
to do the reverse - get the application to trigger a delegate in the
Assembly...

Any one have any suggestions as to where I should look for ?

Regards,

Andy

Apr 10 '06 #1
6 2060
Andrew Neillans wrote:
First off, I apologise if this is mentioned in a MSDN document
somewhere, but I've searched both MSDN and Google and can't find
anything - so thought I'd post.

Ok, now the problem.

I have an application which is dynamically loading a series of
Assemblies (as plugins to extend the main application). This works
wonderfully. However, I now need to provide a way for logging data back
from the assembly to the main application - like a CallBack in C.
However, I can not find anyway to do this -- lots of information on how
to do the reverse - get the application to trigger a delegate in the
Assembly...

Any one have any suggestions as to where I should look for ?


I'm not entirely sure I follow you, unfortunately. Could you provide a
short but complete example program with both assemblies (just tiny ones
- the sample at http://www.pobox.com/~skeet/csharp/plugin.html is the
kind of thing I'm after) and then highlight what you want to happen?

Jon

Apr 10 '06 #2
Hi Jon,

Ok, hopefully this might help :)

Lets say we have this is the main program.

static void Main()
{
DoLogging("Im Alive");
// Main program thread here
// At some point we dynamically load the Assemblies we find, create
instances etc.
}

public void DoLogging(string LogString)
{
// Do something with LogString
}

The DoLogging function does something with the string passed to it - in
my case, it sends it to an internal log server.

Now, the problem is I need to allow the dynamically loaded assemblies
to call the DoLogging function; I could add the DoLogging function and
associated logic to the assemblies however this would increase
dependacies dramatically - which I do not want to do.

So the question is: How can a dynamically loaded assembly access a
function in the parent application?

Clear as mud I know :)

Andy

Apr 10 '06 #3
Use delegates to call your function

CrossAppDomainDelegate delegateA = new CrossAppDomainDelegate(DoLogging);
<YouDomain>.DoCallBack(delegateA);

"Andrew Neillans" wrote:
Hi Jon,

Ok, hopefully this might help :)

Lets say we have this is the main program.

static void Main()
{
DoLogging("Im Alive");
// Main program thread here
// At some point we dynamically load the Assemblies we find, create
instances etc.
}

public void DoLogging(string LogString)
{
// Do something with LogString
}

The DoLogging function does something with the string passed to it - in
my case, it sends it to an internal log server.

Now, the problem is I need to allow the dynamically loaded assemblies
to call the DoLogging function; I could add the DoLogging function and
associated logic to the assemblies however this would increase
dependacies dramatically - which I do not want to do.

So the question is: How can a dynamically loaded assembly access a
function in the parent application?

Clear as mud I know :)


--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Apr 10 '06 #4
Andrew,

Decalre a interface that your all plug-ins will implement. In this interface
decalare a event that is going to be fired whenever something needs to be
logged. Attach the DoLogging method to this event.

This is one possible solution for your problem.
--
HTH
Stoitcho Goutsev (100)

"Andrew Neillans" <ne******@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Hi Jon,

Ok, hopefully this might help :)

Lets say we have this is the main program.

static void Main()
{
DoLogging("Im Alive");
// Main program thread here
// At some point we dynamically load the Assemblies we find, create
instances etc.
}

public void DoLogging(string LogString)
{
// Do something with LogString
}

The DoLogging function does something with the string passed to it - in
my case, it sends it to an internal log server.

Now, the problem is I need to allow the dynamically loaded assemblies
to call the DoLogging function; I could add the DoLogging function and
associated logic to the assemblies however this would increase
dependacies dramatically - which I do not want to do.

So the question is: How can a dynamically loaded assembly access a
function in the parent application?

Clear as mud I know :)

Andy

Apr 10 '06 #5
And to set your string to domain where DoLogging is located, use
<youDomain>.SetData method and Domain.GetData to get it

--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


"Michael Nemtsev" wrote:
Use delegates to call your function

CrossAppDomainDelegate delegateA = new CrossAppDomainDelegate(DoLogging);
<YouDomain>.DoCallBack(delegateA);

"Andrew Neillans" wrote:
Hi Jon,

Ok, hopefully this might help :)

Lets say we have this is the main program.

static void Main()
{
DoLogging("Im Alive");
// Main program thread here
// At some point we dynamically load the Assemblies we find, create
instances etc.
}

public void DoLogging(string LogString)
{
// Do something with LogString
}

The DoLogging function does something with the string passed to it - in
my case, it sends it to an internal log server.

Now, the problem is I need to allow the dynamically loaded assemblies
to call the DoLogging function; I could add the DoLogging function and
associated logic to the assemblies however this would increase
dependacies dramatically - which I do not want to do.

So the question is: How can a dynamically loaded assembly access a
function in the parent application?

Clear as mud I know :)


--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche

Apr 10 '06 #6
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
Apr 10 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Outshined | last post by:
I have a class library where just about every class uses its static initializer to register with a central registry object in the same assembly. I am hoping for some sort of Assembly.Load event...
5
by: Edward Diener | last post by:
This has occurred in MC++, but since there is very little response on that NG, I am also reporting it here in the hope that someone can find me a workaround, and report it to MS. If a __value...
1
by: Kent Rollins | last post by:
I am writing a server and an MMC console app that use remoting to talk to each other. The MMC client and the server share a couple of assemblies: one has the interface that is remoted from the...
4
by: Arnaud Debaene | last post by:
Hello group. I have an app which can load "plugins" assemblies that are described in the registry (each registry entry gives the full path to the plugin ..dll file). The plugins may be anywhere...
1
by: Jon Davis | last post by:
I'm trying to access my application files for a plug-in I'm building for a third party application. I have an assembly with several dependency assemblies. The dependency assemblies are in the...
0
by: Edward Diener | last post by:
If a __value class with an event is put into an assembly, and a __gc class in another assembly attempts to attach its own event handler to the __value class's event of an embedded object of the...
1
by: Silent Ocean | last post by:
Hi All I have following questions regarding C# Assembly and Threading. Let me know the precise answer or lead me to the proper materials. 1. Is memory leakeage possible in .Net Manager...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
0
by: b.tarrance | last post by:
Hi. Looking for any help I can find! I have a brand new virtual server running Windows Server 2003, Enterprise Edition with Plesk Control Panel 7.5 . I just installed the first program on the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.