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
As mentioned in the new documentation for persistable dynamic assemblies in .NET 9 here, to create assemblies for specific TFMs (or assemblies that can actually be used as references in modern C# projects), the new API requires you to create a MetadataLoadContext using the reference assemblies for that TFM. As is already noted by this package's README, this can be challenging!
Adding a bit of documentation that myself and others can point to (or even a helper method to create a MetadataLoadContext like exists for Compilations) would help a lot (I am completely willing to contribute this myself).
This also currently always pulls in a dependency for Microsoft.CodeAnalysis.Common, which if you just want the embedded resources for the assembly files (for example to use for this, or other applications like loading them with AsmResolver.DotNet or MetadataReader) can cause a fairly large transitive dependency that isn't wanted. I completely understand if this is out of scope for what you have here, just let me know if that's the case, I'll probably end up creating a fork of what you have as a separate package.
The text was updated successfully, but these errors were encountered:
Here's some code for those who just want to get spinning with MetadataLoadContext:
usingSystem.Reflection;usingvarcontext=CreateMetadataLoadContextWithAllAssembliesLoaded(fromreferenceinBasic.Reference.Assemblies.Net90.ReferenceInfos.Allselect(reference.FileName,reference.ImageBytes));foreach(varassemblyincontext.GetAssemblies().OrderBy(a =>a.GetName().Name)){Console.WriteLine($"{assembly.GetName().Name}: {assembly.GetExportedTypes().Length} public types");}staticMetadataLoadContextCreateMetadataLoadContextWithAllAssembliesLoaded(IEnumerable<(stringFileName,byte[]ImageBytes)>references){varresolver=newBasicReferenceAssembliesResolver(references);varcontext=newMetadataLoadContext(resolver);foreach(varassemblyNameinresolver.AvailableAssemblyNames)context.LoadFromAssemblyName(assemblyName);returncontext;}internalsealedclassBasicReferenceAssembliesResolver:MetadataAssemblyResolver{privatereadonlyDictionary<string,byte[]>imageBytesByAssemblyName=new(StringComparer.OrdinalIgnoreCase);publicIReadOnlyCollection<string>AvailableAssemblyNames=>imageBytesByAssemblyName.Keys;publicBasicReferenceAssembliesResolver(IEnumerable<(stringFileName,byte[]ImageBytes)>references){foreach(varreferenceinreferences){if(!reference.FileName.EndsWith(".dll",StringComparison.OrdinalIgnoreCase))thrownewArgumentException("Reference assembly file names are all expected to end with .dll.",nameof(references));if(!imageBytesByAssemblyName.TryAdd(reference.FileName[..^".dll".Length],reference.ImageBytes))thrownewArgumentException("Reference assemblies are expected to have unique names.",nameof(references));}}publicoverrideAssembly?Resolve(MetadataLoadContextcontext,AssemblyNameassemblyName){returnassemblyName.Nameis{}name&&imageBytesByAssemblyName.TryGetValue(name,outvarimageBytes)?context.LoadFromByteArray(imageBytes):null;}}
As mentioned in the new documentation for persistable dynamic assemblies in .NET 9 here, to create assemblies for specific TFMs (or assemblies that can actually be used as references in modern C# projects), the new API requires you to create a MetadataLoadContext using the reference assemblies for that TFM. As is already noted by this package's README, this can be challenging!
Adding a bit of documentation that myself and others can point to (or even a helper method to create a MetadataLoadContext like exists for Compilations) would help a lot (I am completely willing to contribute this myself).
This also currently always pulls in a dependency for Microsoft.CodeAnalysis.Common, which if you just want the embedded resources for the assembly files (for example to use for this, or other applications like loading them with AsmResolver.DotNet or MetadataReader) can cause a fairly large transitive dependency that isn't wanted. I completely understand if this is out of scope for what you have here, just let me know if that's the case, I'll probably end up creating a fork of what you have as a separate package.
The text was updated successfully, but these errors were encountered: