You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to load a self-contained directory of assemblies, and have all the references line up. This is extremely common for Unity games.
internalclassProgram{staticvoidMain(string[]args){AssemblyResolverresolver=newAssemblyResolver(args[0]);RuntimeContextcontext=new(newDotNetRuntimeInfo(DotNetRuntimeInfo.NetFramework,newVersion(4,0,0,0)),resolver);List<AssemblyDefinition>assemblies=new();foreach(stringpathinDirectory.EnumerateFiles(args[0],"*.dll")){AssemblyDefinitionassembly=AssemblyDefinition.FromFile(path,newModuleReaderParameters(context));assemblies.Add(assembly);}//Using the assembly list}privatesealedclassAssemblyResolver:AssemblyResolverBase{publicAssemblyResolver(stringdirectory):base(newModuleReaderParameters(directory)){SearchDirectories.Add(directory);}protectedoverridestring?ProbeRuntimeDirectories(AssemblyDescriptorassembly){returnnull;}}}
I know mscorlib gets loaded twice because I have TypeDefinition objects that are signature equal (SignatureComparer), but not reference equal.
Ideal Solution
I propose we add AssemblyDefinition.FromFiles and ModuleDefinition.FromFiles.
Workaround
This is how I'm currently working around the issue.
The problem here is that currently AssemblyDefinition::FromXXX is kind of independent of RuntimeContext and its assembly resolver. It always produces a new instance of AssemblyDefinition, regardless of whether this was already added to a context's resolver cache or not (eg via dependency resolution).
I currently see two solutions to this problem:
We change the behavior of AssemblyDefinition::FromXXX to have it first check the cache of the RuntimeContext's assembly resolver, see if there's already an assembly with the same signature and if so return that one instead of a new instance. This results in 0 changes for end-users, but may be confusing if loading a second instance is actually required, thus costing in predictability of the API.
We leave AssemblyDefinition::FromXXX as is, but instead add a third way of loading assemblies into the RuntimeContext by adding LoadFromXXX methods to RuntimeContext that do this check. This keeps things decoupled and arguably results in a more predictable API, at the cost of yet another API for loading files to be introduced.
Personally I am of the opinion that option 2 is best, unless there are other options I haven't explored yet.
In regards to 1, if a runtime context is being provided, it would be intuitive for it to return an already loaded assembly instead of loading a duplicate into the runtime context.
In regards to 2, I would love a RuntimeContext::LoadFromFolder.
Problem
I want to load a self-contained directory of assemblies, and have all the references line up. This is extremely common for Unity games.
I know
mscorlib
gets loaded twice because I haveTypeDefinition
objects that are signature equal (SignatureComparer), but not reference equal.Ideal Solution
I propose we add
AssemblyDefinition.FromFiles
andModuleDefinition.FromFiles
.Workaround
This is how I'm currently working around the issue.
The text was updated successfully, but these errors were encountered: