Under what conditions can I swap out an assembly that's statically referenced at compile time with a different one for use at runtime? For example:
App Assembly:
- References Common Assembly
- References
ServiceProviderFactory
in Service Assembly
Common Assembly:
- Defines
IServiceProvider
Service Assembly:
- Provides
ServiceProviderFactory
(a factory forIServiceProvider
s) - References Common Assembly
Given the above, I'd like to be able to swap out the Service assembly at install time but I don't know under what conditions the new Service assembly will continue to load.
I believe the following must hold:
- Same assembly name,
- Weakly named assembly or same version number in a strongly named assembly, and
ServiceProviderFactory
signature remains constant.
Note: it seems better to use reflection and search the service assembly for an implementation of IServiceProvider
than to rely on some ServiceProviderFactory
with a fixed signature being present but I'm still interested in the a开发者_Go百科nswer to the above.
If it's strong-named, then you can't really use assembly redirection for anything other than version differences. However, you can use the AppDomain.AssemblyResolve event to supply an assembly in response to a failure to bind the requested assembly.
I actually tried this as a proof of concept when I tried to support .NET 2.0 assemblies that referenced a CodePlex build of MEF in a .NET 4 application that referenced the released build of MEF. Here's a link to my question that someone responded to with that evil trick.
However, this is all very tricky and I wouldn't feel comfortable doing it in a real application. Can you extract your interfaces into a separate assembly then use some kind of dependency injection such as MEF or Unity to dynamically load the types that implement those interfaces?
精彩评论