xsharp.eu • Dynamically loading XSharp assemblies
Page 1 of 1

Dynamically loading XSharp assemblies

Posted: Thu Jan 26, 2017 3:22 pm
by ArneOrtlinghaus
When trying to load XSharp assemblies with the command VulcanLoadLibrary I received error messages. When substituting this by the function below it works, at least for assemblies without the automatically called init procedures.

But I have seen that it is better to call all initialization manually in the program.
I have seen that runtime errors in the init procedures are very hard to identify, whereas when calling them in the program the runtime errors are displayed with reasonable messages on the screen.

function xsLoadLibrary(libPath as string) as System.Reflection.Assembly
local pdll as System.Reflection.Assembly
pdll := System.Reflection.Assembly.LoadFrom(libPath)
return pdll

Dynamically loading XSharp assemblies

Posted: Thu Jan 26, 2017 4:31 pm
by Chris
Hi Arne,

Thanks for reporting this, it is a bug (well, compatibility issue) in the compiler, as it generates assemblies with slightly different layout than VulcanLoadLibrary() expects. Should be easy enough to fix.

Chris

Dynamically loading XSharp assemblies

Posted: Thu Jan 26, 2017 4:31 pm
by wriedmann
Hi Arne,

use Reflection! Since X# currently does not need initializations like the Vulcan runtime, you should be able to do it.

I'm doing this regularly:

Code: Select all

foreach cDLLName as string in oDLLs
  cFullPath := Path.Combine( cPath, cDLLName )                           
  oAssembly := Assembly.LoadFrom( cFullPath )
  oClasses := Utility.AssemblyClasses( oAssembly, "ProdConfigBase.IConfiguration" )
  foreach cClassName as string in oClasses
    oConfiguration := ( IConfiguration ) oAssembly:CreateInstance( cClassName )
    if oConfiguration != null
      ++nLoaded
      oConfiguration:Initialize( oWindow, ProgSettings.Language )
      if oConfiguration:LastException != null
        MessageBox.Show( string.Format( e"Error loading configuration DLL {0}nn{1}nn{2}", cDLLName, oConfiguration:LastException:Message, oConfiguration:LastException:StackTrace ) )  
      endif
      ProgSettings.ProduktConfigurations:Add( oConfiguration )
    endif
  next
next
Wolfgang