Replies: 1 comment
-
Something to keep in mind is that each submission (i.e. each time you run a cell, whether the same cell or a different cell) results in a new assembly being generated in-memory by C# scripting. Each submission's assembly references the previous, and what I'm inferring is happening is that you'll only see these references in the @tmat can undoubtedly add more detail. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The package and version I'm asking about:
Question
For an application at work we have a class which uses
Assembly.LoadFrom(<path to dll>).ExportedTypes
to create a list of all the public types that our assemblies export. This class contains a method likeGetExportedType<T>(string typeName)
to retrieve a type from this list, to construct a new instance of it and to cast it toT
. In this method we go over all the exported types, compare the names and checktypeof(T).IsAssignableFrom(type)
to see whether there is a valid match.For
IsAssignableFrom
to work properly it is required for bothtypeof(T).Assembly
andtype.Assembly
to be from the same assembly load context. Currently we rely on both assemblies being in the default assembly load context (i.e.AssemblyLoadContext.Default
), but that assumption breaks when using our application in a .NET Interactive C# notebook, because notebook cells use their own assembly load context. However, if we callAssembly.LoadFrom(<path to dll>)
for all our application's assemblies, the next time a notebook cell uses our application it magically loads our application's assemblies from the default assembly load context.Although this fixes our issue, we're wondering what's going on here and whether we can improve our workaround. Consider the following minimal example:
Lets say I have a library called
ALCPrinter
containing one class:Now consider the following three example notebooks:
Example 1:
Example 2:
Example 3 after running the cell for the first time:
Example 3 after running the cell for the second time:
Repository: jmerle/alc-printer
Why does the assembly load context differ in these examples?
Ideally I'd like it to print that it's in the default assembly load context like in example 2, but using only a single notebook cell like in examples 1 and 3. Is that possible?
Beta Was this translation helpful? Give feedback.
All reactions