// internal for testing
        internal static Stream GetWin32ResourcesInternal(CommonMessageProvider messageProvider, CommandLineArguments arguments, Compilation compilation, out IEnumerable <DiagnosticInfo> errors)
        {
            List <DiagnosticInfo> errorList = new List <DiagnosticInfo>();

            errors = errorList;

            if (arguments.Win32ResourceFile != null)
            {
                return(OpenStream(messageProvider, arguments.Win32ResourceFile, arguments.BaseDirectory, messageProvider.GetFieldOrProperty <int>("ERR_CantOpenWin32Resource"), errorList));
            }

            using (Stream manifestStream = OpenManifestStream(messageProvider, compilation.Options.OutputKind, arguments, errorList))
            {
                using (Stream iconStream = OpenStream(messageProvider, arguments.Win32Icon, arguments.BaseDirectory, messageProvider.GetFieldOrProperty <int>("ERR_CantOpenWin32Icon"), errorList))
                {
                    try
                    {
                        return(compilation.CreateDefaultWin32Resources(true, arguments.NoWin32Manifest, manifestStream, iconStream));
                    }
                    catch (Exception ex) when(ex.GetType().FullName == "Microsoft.CodeAnalysis.ResourceException")
                    {
                        errorList.Add(ReflDiagnosticInfo.ctor(messageProvider, messageProvider.GetFieldOrProperty <int>("ERR_ErrorBuildingWin32Resource"), new[] { ex.Message }));
                    }
                    catch (OverflowException ex)
                    {
                        errorList.Add(ReflDiagnosticInfo.ctor(messageProvider, messageProvider.GetFieldOrProperty <int>("ERR_ErrorBuildingWin32Resource"), new[] { ex.Message }));
                    }
                }
            }

            return(null);
        }
Exemple #2
0
        // internal for testing
        internal static Stream GetWin32ResourcesInternal(CommonMessageProvider messageProvider, CommandLineArguments arguments, Compilation compilation, out IEnumerable <DiagnosticInfo> errors)
        {
            List <DiagnosticInfo> errorList = new List <DiagnosticInfo>();

            errors = errorList;

            if (arguments.Win32ResourceFile != null)
            {
                return(OpenStream(messageProvider, arguments.Win32ResourceFile, arguments.BaseDirectory, messageProvider.ERR_CantOpenWin32Resource, errorList));
            }

            using (Stream manifestStream = OpenManifestStream(messageProvider, compilation.Options.OutputKind, arguments, errorList))
            {
                using (Stream iconStream = OpenStream(messageProvider, arguments.Win32Icon, arguments.BaseDirectory, messageProvider.ERR_CantOpenWin32Icon, errorList))
                {
                    try
                    {
                        return(compilation.CreateDefaultWin32Resources(true, arguments.NoWin32Manifest, manifestStream, iconStream));
                    }
                    catch (ResourceException ex)
                    {
                        errorList.Add(new DiagnosticInfo(messageProvider, messageProvider.ERR_ErrorBuildingWin32Resource, ex.Message));
                    }
                    catch (OverflowException ex)
                    {
                        errorList.Add(new DiagnosticInfo(messageProvider, messageProvider.ERR_ErrorBuildingWin32Resource, ex.Message));
                    }
                }
            }

            return(null);
        }
        public Assembly Compile(Microsoft.CodeAnalysis.Compilation compilation, string outputPath)
        {
            var pdbPath    = Path.ChangeExtension(outputPath, "pdb");
            var xmlDocPath = Path.ChangeExtension(outputPath, "xml");

            using (MemoryStream dllStream = new MemoryStream(), pdbStream = new MemoryStream(), xmlStream = new MemoryStream())
            {
                using (var win32ResStream = compilation.CreateDefaultWin32Resources(
                           versionResource: true, // Important!
                           noManifest: false,
                           manifestContents: null,
                           iconInIcoFormat: null))
                {
                    var result = compilation.Emit(
                        peStream: dllStream,
                        pdbStream: pdbStream,
                        xmlDocumentationStream: xmlStream,
                        win32Resources: win32ResStream);

                    if (!result.Success)
                    {
                        var failures = result.Diagnostics.Where(diagnostic =>
                                                                diagnostic.IsWarningAsError ||
                                                                diagnostic.Severity == DiagnosticSeverity.Error);

                        foreach (var diagnostic in failures)
                        {
                            Logger.Error(diagnostic);
                        }

                        return(null);
                    }

                    _fileSystem.WriteAllBytes(outputPath, dllStream.ToArray());
                    _fileSystem.WriteAllBytes(pdbPath, pdbStream.ToArray());
                    _fileSystem.WriteAllBytes(xmlDocPath, xmlStream.ToArray());
                    return(_assemblyLoader.LoadFrom(outputPath));
                }
            }
        }