1

I have several issues with several SDK's comming from OEM manufacturers for specific devices. SDK is usually based on C or C++ dll, so I have a lot of Marshaling going around (a lot===YOU CAN'T EVEN IMAGINE). Problem start with next version of SDK when they extend some functions or some structures, they effectively break compatibility. In past I have made copy of our library supporting their device and start making changes to support new SDK. But each time our library was only for specific SDK, and upgrades of our systems were tough (Installation script if one heavy thing also ~ 3 GB install).

I have 78 projects in solution, commonly 4-5 libraries for each OEM Manufacturer, this is without any service tools. And Yesterday I said NO MORE. Started research on subject how to recompile C# code in runtime and reload/replace same assembly without quiting App. And the result is the following: - Class file that defines external C/C++ dll API was referenced from external Project referencing only System.dll. And me being insane I've already had each SDK version changes wrapped around #if #elif #endif so I could recompile last version of our library to support previous version of SDK. But that was maybe only once done, I've used #defines along with CSharpCodeProvider to recompile this assembly in runtime. Idea was like this:

  • Application loading ...
  • Open main SDK file get file version (extract version and identify it).
  • Load original External Assembly in new AppDomain (so I could destroy domain later).
  • Extract current version from external assembly.
  • Destroy new AppDomain to release hook from external assembly.
  • If versions mismatch, recompile external assembly (source code for external assembly is embedded within parent assembly), and replace original DLL with just compiled one.
  • Continue loading application...

So far this test approach works on one live demo system, and I was amazed. Switching from one to another SDK was flawless without any hick-ups. And also code recompiles it self only when SDK version changes. So with safe guard I could say this is my first Metamorphic code I've wrote, that recompiles/changes it self from runtime.

Unfortunately this approach requires me to add one more Project for each OEM Manufacturers SDK. Which effectively kills my first though why I said NO MORE. True I now have only two libraries to maintain per one OEM manufacturer, and there will be no more projects added after this. But...

I wonder is there better approach which could allow me to replace DLL of currently loaded assembly in runtime from true within same assembly? Or change executing code on "fly" each time, this mainly includes Marshaled function, classes, structures, constants, ...?

Please notice code should be maintained from within same project without any externals. Also please notice this project exposes only hard-coded interface to "outside" world (Interface is referenced Interface only project - is more complex than I wrote). But this "outside" world is blind to any OEM specific stuff, which was the point using interface to have exactly same behavior across any OEM Device.

Any ideas? thoughts? suggestions?

Ped7g
  • 16,236
  • 3
  • 26
  • 63
SoLaR
  • 778
  • 7
  • 22
  • You're doing what I would do, I'm afraid. – hoodaticus Feb 09 '17 at 20:09
  • @hoodaticus: Well MSScriptingControl does compile in memory TypeInfo for each JavaScript Com object exposed trough CodeObject property. Was wondering would it be possible something similar in .Net, to build types in memory and access them dynamically. – SoLaR Feb 19 '17 at 23:15
  • you can use Reflection.Emit to generate classes and methods on the fly. Not sure how useful it would be above your current approach. – hoodaticus Feb 20 '17 at 15:31
  • wouldn't it make code little dirty, also hardcoded? In mean time I was looking at .netmodule was thinking to compile all .net modules with different compile condition flags and later relink assembly on need. Or maybe full assemblies, so I wouldn't need to resource source code of API within parent assembly, also I would essentially remove compile from target machine. This two approaches makes more promisses, than Reflection.Emit – SoLaR Feb 23 '17 at 07:38

0 Answers0