views:

504

answers:

1

Let me give a scenario then see if anyone has a solution / work-around.

I've got a library (DLL) that contains only classes. In another library, I've got interfaces that the classes in the first library implement. Clients will always reference only one version of the library containing the interfaces (always the latest one) - and only additions will be made to those interfaces (more interfaces / methods). Also, there will only be one version of that interfaces library - so that clients don't need to update references (the interfaces library will be loaded through reflection - so the usual probing rules do not apply, we can only load what the client specifically asks for). The library implementing the interfaces will have multiple versions that different clients reference at the same time.

The problem occurs when a client assembly is referencing an old version of the implementation library. It creates an object from a class type in the implementation library - then passes it to another client assembly. That client assembly uses the latest (and only) version of the interfaces library and attempts to cast the object passed to it with the appropriate interface type. This cast fails with the exception 'System.TypeLoadException: Method X in type Y from assembly Z does not have an implementation'. I expected this exception because in the new version of the interface (same version number - old library clobbered), method X is defined, but even though the old version of the object that is passed contains metadata that says it implements the corresponding interface, the mapping fails on the new interface method X because it doesn't have any implementation in that class.

So, my question is this; Is there a way to have a client use that interface, but only cause an exception when a method that is not implemented on the referenced object is called (rather than the exception being generated when the referenced object is cast to the newer interface type)?

By the way - late binding isn't an option because we want intellisense / compile time type checking ... Also, I know how to do this with function pointers and wrapper classes - I was just hoping for a better solution that is more in tune with regular .NET types.

Thanks -- Any help is greatly appreciated

+1  A: 

You could use an abstract (MustInherit) Class instead of an Interface in the library containing the interfaces and set the methods and properties as Overridable. This way even if the methods just threw a not implemented exception there would always be an implementation for every method in the interface library. By making the methods Overridable rather than MustOverride should avoid the TypeLoadException.

bstoney
This is a great - it's not an interface, but it works perfectly. Virtual methods - kind of mad at myself for not thinking of this. Thanks for helping me see the forest from the trees.
Glad to help, took me a while to think of an answer that would be workable. The problem was an interesting one.
bstoney