How to add additional info to a delegate instance? (mimic java EventListenerProxy)

?

-

Hi to All,

Is there any trick for adding additional runtime info (for example a string)
to a delegate instance?

I would like to mimic the Java EventListener - EventListenerProxy,
PropertyListener - PropertyListenerProxy
semantics. I can replace the PropertyListener class with a multicast
delegate, let's say PropertyChangeDelegate.
The problem that PropertyListenerProxy should be also a delegate, but it has
an additional string property, which can be set by the subscriber, and
publisher can filter (or anything) by it.

Of course I can mechanically port the Java event handling semantics, but in
this case I can not use the high level
functionality which is out of the box with .NET multicastdelegate, and I
must reimplement it :-(

Any ideas?
Thx for answers
 
N

Nicholas Paldino [.NET/C# MVP]

AFAIK, there isn't anything in the framework to do this. Doing
something of this nature isn't that hard though. Since you can attach a
delegate to any method that has a matching signature, it would be easy to
create a wrapper that will act as storage for the extra information, and put
the event handler logic in there (along with references to whatever else it
needs).
 
?

-

Hi Nicholas,

Thx for your answer.

The problem with the wrapper solution is that the wrapper (or proxy as the
Java implementation calls it) should be itself type compatible with the
contained delegate, with other words, it should be a delegeate also.

This is important because we must put the proxy to the invocation list of
the publisher. (We must Combine it (or +=) to the multicast list.

If we can not do that, then we must reimplement the multicast infrastucture,
which is far more complicated as if looks at first sight.

horo




Nicholas Paldino said:
AFAIK, there isn't anything in the framework to do this. Doing
something of this nature isn't that hard though. Since you can attach a
delegate to any method that has a matching signature, it would be easy to
create a wrapper that will act as storage for the extra information, and
put the event handler logic in there (along with references to whatever
else it needs).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


- said:
Hi to All,

Is there any trick for adding additional runtime info (for example a
string) to a delegate instance?

I would like to mimic the Java EventListener - EventListenerProxy,
PropertyListener - PropertyListenerProxy
semantics. I can replace the PropertyListener class with a multicast
delegate, let's say PropertyChangeDelegate.
The problem that PropertyListenerProxy should be also a delegate, but it
has an additional string property, which can be set by the subscriber,
and publisher can filter (or anything) by it.

Of course I can mechanically port the Java event handling semantics, but
in this case I can not use the high level
functionality which is out of the box with .NET multicastdelegate, and I
must reimplement it :-(

Any ideas?
Thx for answers
 
N

Nicholas Paldino [.NET/C# MVP]

Horo,

That's the thing, it is type compatable, since you are using the same
delegate type. The delegate is identified by the signature, so you can have
a wrapper of a different type which has a method with the same signature
which is wrapped by the delegate, and then attach that to the event.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

- said:
Hi Nicholas,

Thx for your answer.

The problem with the wrapper solution is that the wrapper (or proxy as the
Java implementation calls it) should be itself type compatible with the
contained delegate, with other words, it should be a delegeate also.

This is important because we must put the proxy to the invocation list of
the publisher. (We must Combine it (or +=) to the multicast list.

If we can not do that, then we must reimplement the multicast
infrastucture, which is far more complicated as if looks at first sight.

horo




Nicholas Paldino said:
AFAIK, there isn't anything in the framework to do this. Doing
something of this nature isn't that hard though. Since you can attach a
delegate to any method that has a matching signature, it would be easy to
create a wrapper that will act as storage for the extra information, and
put the event handler logic in there (along with references to whatever
else it needs).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


- said:
Hi to All,

Is there any trick for adding additional runtime info (for example a
string) to a delegate instance?

I would like to mimic the Java EventListener - EventListenerProxy,
PropertyListener - PropertyListenerProxy
semantics. I can replace the PropertyListener class with a multicast
delegate, let's say PropertyChangeDelegate.
The problem that PropertyListenerProxy should be also a delegate, but it
has an additional string property, which can be set by the subscriber,
and publisher can filter (or anything) by it.

Of course I can mechanically port the Java event handling semantics, but
in this case I can not use the high level
functionality which is out of the box with .NET multicastdelegate, and I
must reimplement it :-(

Any ideas?
Thx for answers
 
J

Jon Shemitz

The problem with the wrapper solution is that the wrapper (or proxy as the
Java implementation calls it) should be itself type compatible with the
contained delegate, with other words, it should be a delegeate also.

No, you misunderstood the suggestion. Let's say you have a CoreClass
that has a method

void Example(int N)

that you wanted to create a delegate for. Plus, you wanted the
delegate to have access to an extra string. You'd just create a class
like

class Wrapper
{
public Wrapper(CoreClass Core, string ExtraString)
{
this.Core = Core;
this.ExtraString = ExtraString;
}

private CoreClass Core;
private string ExtraString;

public void Example(int N)
{
if (ExtraString == "Do it!") // or whatever
Core.Example(N);
}
}

and pass a delegate to Wrapper.Example instead of CoreClass.Example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top