IRoslynRepository.RegisterAssembly from stream

Would it be possible to provide an overload of “RegisterAssembly” where the assembly bytes can be provided from stream?

The reason why I ask: in the .NET6 version of our app, we moved from “Microsoft.CodeDom.Providers.DotNetCompilerPlatform” to direct Roslyn usage to compile a DLL from our code. One kind of scripts in our app makes use of other scripts which are compiled on the fly to a utility DLL in the temp dir. And this DLL is added to the references of AlterNet. After taking the .NET6 version in production, the applocker in our cloud environment reports warnings whenever we access the utility dll (but it seems to work anyway).

This means that in our Roslyn wrapper, we would have to use “MetadataReference.CreateFromStream” instead of “CreateFromFile”.
Would the same be possible with “IRoslynRepository.RegisterAssembly”? I see that internally you also end with a MetadataReference, so I hope you can enhance it?

Best regards

Wolfgang

Hello Wolfgang,

You can use RoslynRepositoryRoslynRepository.AddImageReference with the code like this:

            byte[] assemblyImage = null;
            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("some.dll");
            if (stream != null)
            {
                assemblyImage = ReadFully(stream);
                if (assemblyImage != null)
                    csParser1.Repository.AddImageReference(assemblyImage);
            }

        private byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }

                return ms.ToArray();
            }
        }

Best regards,
Andrew

Hi Andrew,

great, thanks for the reply. I did not look for “AddImageReference”.

Best regards

Wolfgang

Just to complete this: if the xml documentation for the assembly is also loaded from a byte array, you can use this code to register both:

    DocumentationProvider docProvider = XmlDocumentationProvider.CreateFromBytes(xmlDocBytes);
    PortableExecutableReference metaDataRef = MetadataReference.CreateFromImage(assemblyBytes,
      MetadataReferenceProperties.Assembly, docProvider);
    csParser1.Repository.AddReference(metaDataRef);

Best regards

Wolfgang