I have three components:
- utility library (Processor.dll): Only knows about Element library
- Element library (IElement): Doesn't know about any library
- App: Knows about both libraries
App calls the Processor library and passes it a class of type:
ClassA : IElement
ClassA is serialized before being passed to Processor. Processor is a base library and does not know about class types such as ClassA. It does know about IElement however. Processor will deserialize the IElement passed to it, which is of type ClassA.
The issue is that an interface cannot be deserialized. But Processor does not know about ClassA and should not. How can I get a reference to the passed in object in this case?
One way to handle this is the create a SerializationBinder implementation that loads classA, then pass a reference to an instance of your binder to Processor.dll so that Processor.dll can use your binder implementation for deserialization. This will allow you to keep the code that references ClassA in the App module (the SerializationBinder implementation will of course have to be defined in the App module).
Here's an example: Given this interface in the Element library
you would define your processor like this:
In this example, I'm using a very, very simple serialization binder. Note that this must be defined in the App assembly, that's why you don't need a reference to
ClassA
anywhere besides App.Then we bring it all together in the App assembly:
See this This MSDN example for another example of a SerializationBinder.