[导入]How can I subscribe to events exposed by a remoted object?

     Delegates require information about the type that the method is associated with in order to make a call.  In a single app-domain, this isn't a problem, because the server (the object firing the events) has access to the type information for the client (which has the event handlers) by way of the delegate.

     However, during remoting, the server most likely does not have any information about the client.  If you want events from the server to fire in the client app domain, then the client must derive from MarshalByRefObject.  This is required so that the server will call back into the client, as opposed to a copy of the client object that is passed to the server.

     A simple way to do this is to place a copy of the client assembly in the same directory where the server directory is.  While this will work, it is not an elegant solution, as it exposes the client type unecessarily.

     A more elegant solution (albiet more complex), is to have a single assembly that is referenced by both the client and the server.  This assembly will have a shim class which will expose methods that share the signature of the events that you want to shim.  The assembly will also provide an interface with methods that share the signature of the events as well.  The shim will then take a reference to the interface, and the methods will aggregate the call to the interface that was passed in (I recommend the constructor as the place to pass the interface implementation).

     Finally, the client object will implement the interface.  When subscribing to the events that the server exposes, attach the delegates to the methods on the shim, which will be passed your client object's implementation.

     It is also important to note that in order for this to work, the TypeFilterLevel property on the sink provider for the server needs to be set to TypeFilterLevel.Full.


文章来源: http://blogs.msdn.com/csharpfaq/archive/2004/04/28/122691.aspx

你可能感兴趣的:(object)