Home > Back-end >  How to run managed code on different version assemby
How to run managed code on different version assemby

Time:09-21

I'm working with one application that that has and C# API. This program has different versions of it. But the api stays the same through its versions.

So i have written a managed code to one of its versions, and now i want to run the same code at different version of the application at runtime where i exactly know witch version of the app is running.

Question is: Is it possible to replace assembly version and dll location at run time without writing unmanaged code using reflections?

CodePudding user response:

Yes, you can use Assembly.LoadFrom to load an assembly. You can then use reflection to go thru the types of said assembly and call methods.

To avoid needing to use reflection for everything there should be a shared interface-assembly that define your api. There should also be a single entry point to the API. So you can use reflection find the class that implements the entry-interface, create an instance of this class and cast it to the interface. That lets the rest of the code use actual types.

You still need to be careful however, if there is any miss match between the interface and the actual types, you will get an runtime exception. You will not get an exception when the interface method is called (as might be expected), but when the method that calls the interface method is called. This due to the jitter resolving types when a method is compiled, and this is done the first time it is called.

  • Related