I've been playing around with a way to dynamically load .NET Core dlls from a .NET Framework project. It has been interesting.
The .NET 8 project currently has this in it:
Code:
Public Shared Function LoadCoreRearingUnitPlugins(fileName As String) As Type()
Dim asmbl = Assembly.LoadFrom(fileName)
If asmbl IsNot Nothing Then
Return asmbl.GetExportedTypes
Else
Return New Type() {}
End If
End Function
Originally, I passed in a System.Reflection.Assembly object, but there was a significant change between the Framework version of that object and the Core version of the object. The code I put into the Framework project can be condensed down to this:
Code:
Private Sub LoadShapes()
Dim aDLL As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(fl)
If aDLL IsNot Nothing = Nothing Then
Dim tArr As Type()
Try
tArr = aDLL.GetExportedTypes
Catch ex As System.IO.FileNotFoundException
tArr = CoreLoader.LoadMethods.LoadCoreRearingUnitPlugins(fl)
Continue For
End Try
'Etc.
This is right off in a method that gets called from elsewhere. I found that if I loaded a Core dll using that code, then aDLL.GetExportedTyppes would throw a FileNotFoundException (or possibly something else, I realize I haven't fully validated that). The way I originally had LoadCoreRearingUnitPlugins working, the call would be made, but would immediately fail. I was passing aDLL to the method, and the difference between the Framework Assembly and the Core Assembly broke the code.
Therefore, I figured I could pass the file name to the method, and changed the method so that it loads the assembly itself (as shown in the first code snippet). By doing that, I would get the Core version of Assembly, not the Framework version. That seemed like a good idea, but it failed in a way I have never actually seen code fail before.
I can't even step into the LoadShapes method. Even the attempt to call the method throws a FileNotFoundException. I can't even step into the first, innocent, line. This is because of the call to LoadCoreRearingUnitPlugins. I have confirmed that because the code works fine so long as I comment that code out. Note that the code DID work, until I added the reference to System.Reflection.Assembly to the Core project. So, it was adding that reference that means the very call to LoadShapes throws an exception. The code doesn't even make it to the first line if that call to LoadCoreRearingUnitPlugins is found anywhere in the function.
I'm not sure what to make of this, and whether there is any way to get around it. A Framework project can reference a Core dll, but if that Core dll references System.Reflection.Assembly, then a method in the Framework project that includes a call to the Core dll appears to throw an exception.
The ultimate goal was to be able to mix and match dynamically loaded dlls from either Core or Framework. I was thinking it wasn't possible, but I thought I had found a way around it by letting the Framework project load Framework plugins and have a Core project that loaded Core plugins. It's looking like doing so just isn't possible.
Any suggestions?