Use assembly loading context to isolate loaded assemblies from AppDomain #714 #716
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Mapster.Tool update to solve issue #714
TL; DR
Replace Assembly.LoadFrom calls with something more robust that prevents assembly/type collisions between the assemblies in the running Mapster.Tool process (such as .NET framework assemblies) and the scanned assemblies - while also preserving the ability to scan for attributes that are in the Mapster assembly (using share Assembly resolution).
See Pertinent MSFT Docs
More detail
Added an AssemblyLoadContext implementation for dynamically loading assemblies when scanning in an isolated way so that they don't have collisions with all of the assemblies loaded by Mapster by default (for example framework assemblies such as System.Text.Json).
The implementation also allows the setting of a subset of assembly names that will be shared with AssemblyLoadContext.Default (the default context that all assemblies are resolved from if no custom ALC were created). Some scanned types need to be owned by the default assembly context such as those from the "Mapster" assembly.
The end result is that we can resolve types from the target assembly being scanned, and we can do things like .GetCustomAttribute() and the attribute will be found because it comes from a shared assembly across the default (runtime) AssemblyLoadContext and the custom one used to avoid collisions.