Cheryl wrote:
[...]
However, with this design, all objects receive the callback once the
server callback due to the multicasting property. If I want only the
object which sends out the command to receive the callback, how should I
implement that?
You would need a collection of delegate instances in the static class,
maybe a Dictionary<where the key is the object with the delegate and
the value is the delegate itself. Then given an instance of a
testobject, you can use the testobject reference as the key for the
Dictionary<to retrieve the callback delegate.
If you are already keeping some sort of collection of the delegate
instances anyway, then you could store the delegate from the instance in
there as well somehow. Note, of course, that you simply cannot use the
event paradigm in this case, since you only want to call a specific
instance. You need to store the delegate reference as a per-instance of
the testobject, and that's just not possible with the event.
That said, it appears from your code that every testobject instance
subscribes to the event. It seems to me that it might make more sense
in this instance to declare a simple interface that the testobject class
implements. Then the static class can just call that interface method
instead.
For example:
static class ServerClass
{
public interface ICallback
{
public Callback();
}
}
class testobject : ICallback
{
public ICallback.Callback()
{
}
}
Then in the ServerClass, when you would have had something like this:
Server.callback handler = Oncallback;
if (handler != null)
{
handler(...);
}
which winds up calling all of the testobject instances, you'd have
something like this:
ICallback icallback = (ICallback)testobjectInstance;
icallback.Callback();
which would take a testobject instance references by
"testobjectInstance", get the interface that contains the callback
method from it, and then call the Callback() method in the interface.
Pete