Closed cwollenhaupt closed 4 years ago
Hmmm... you're right. Just an omission, but you can call this directly for now. I'm adding a function to wwDotnetBridge.prg.
************************************************************************
* CreateInstanceFromFile
****************************************
*** Function: Creates an instance of an assembly from a file on disk
*** without having to call LoadAssembly first.
*** Assume: Be careful with this as it doesn't load dependencies.
*** The safer path is to use LoadAssembly then load the type.
*** Mainly useful for loading things like addins out of diff
*** non-application folders.
*** Pass:
*** Return: object instance on success null or exception on failure
************************************************************************
FUNCTION CreateInstanceFromFile(lcAssemblyFilename, lcTypename, lvParm1, lvParm2, lvParm3)
this.SetError()
loBridge = this.oDotnetBridge
IF ISNULL(loBridge)
RETURN NULL
ENDIF
lcAssemblyFileName = FULLPATH(lcAssemblyFileName)
lnParmCount = PCOUNT()
DO CASE
CASE lnParmCount = 3
loObject = loBridge.CreateAssemblyInstanceFromFile_OneParm(lcAssemblyFilename, lcTypename, lvParm1)
CASE lnParmCount = 4
loObject = loBridge.CreateAssemblyInstanceFromFile_TwoParms(lcAssemblyFilename, lcTypename, lvParm1, lvParm2)
CASE lnParmCount = 5
loObject = loBridge.CreateAssemblyInstanceFromFile_ThreeParms(lcAssemblyFilename, lcTypename, lvParm1, lvParm2, lvParm3)
OTHERWISE
loObject = loBridge.CreateAssemblyInstanceFromFile(lcAssemblyFilename, lcTypename)
ENDCASE
RETURN loObject
ENDFUNC
* CreateInstanceFromFile
updated with fixed code
Some of these are just lifted from my Reflection utilities. They are not used for anything but they may be useful.
I wouldn't recommend using this except for a very specific use case. CreateAssemblyIntanceFromFile has very different load characteristics from standard load behavior (throws different exceptions and resolves assemblies differently) so you almost always need special assembly resolvers in order to get dependencies loaded. If you can use LoadAssembly()
instead as that will load all dependencies automatically.
I use this in special load scenarios like loading assemblies from Addin folders where I don't want all the dependencies to get also loaded but only the types I'm interested in.
IOW it's not terribly useful except in very special cases.
Just to provide an update... The problem I actually wanted to solve was that I loaded two DLLs and the second DLL would complain that it cannot load the first one, even though I just successfully loaded it one code line earlier. This would, of course, only happen at runtime, never when debugging my FoxPro application. Turns out that I never considered AssembyLoadContexts because this stuff usually works automatically. Your response helped me figuring this out so I indeed do not need to use this particular method.
Hi Rick,
I noticed that the C# code has a method CreateAssemblyInstanceFromFile which calls AppDomain.CreateInstanceAndUnwrap instead of searching all assemblies for the specified type. But it's never called from the wwDotNetBridge.prg file or anywhere in the C# code.
Is that just unfinished work or did you run into problems with this method?