Пример #1
0
        public async Task <(bool succeeded, IEnumerable <IDiagnosticInfo> diagnostics)> GenerateAssemblyAsync(
            string sourceCodeFile
            , string fileName
            , string assembliesPath)
        {
            var referenceAssembliesPath = Path.GetDirectoryName(typeof(string).Assembly.Location);
            var references = new List <MetadataReference>
            {
                MetadataReference.CreateFromFile(Path.Combine(referenceAssembliesPath, "netstandard.dll")),
                MetadataReference.CreateFromFile(Path.Combine(referenceAssembliesPath, "System.Runtime.dll")),
                MetadataReference.CreateFromFile(typeof(string).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ExportAttribute).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(DbContext).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(KeyAttribute).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(IBusinessRepositoryFactory).Assembly.Location),
            };
            string sourceCode;

            using (var fileStream = new FileStream(sourceCodeFile, FileMode.Open, FileAccess.Read))
                using (var reader = new StreamReader(fileStream))
                {
                    sourceCode = await reader.ReadToEndAsync();
                }
            if (!CSharpLanguage.CompileToAssembly(sourceCode, Path.GetFileNameWithoutExtension(fileName), fileName, references, out var diagnostics))
            {
                return(false, diagnostics?.Select(x => new DiagnosticInfo {
                    Message = x.GetMessage()
                }));
            }
            return(true, null);
        }
Пример #2
0
        public async Task <CodeGenerationResult> GenerateAssemblyAsync(
            string sourceCodeFile
            , string assymblyFileName
            , string assembliesPath)
        {
            var referenceAssembliesPath = Path.GetDirectoryName(typeof(string).Assembly.Location);
            var localPath  = Path.GetDirectoryName(typeof(ExportAttribute).Assembly.Location);
            var references = new List <MetadataReference>
            {
                MetadataReference.CreateFromFile(Path.Combine(referenceAssembliesPath, "netstandard.dll")),
                MetadataReference.CreateFromFile(Path.Combine(referenceAssembliesPath, "System.Runtime.dll")),
                MetadataReference.CreateFromFile(typeof(string).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ExportAttribute).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(DbContext).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(KeyAttribute).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(IBusinessRepositoryFactory).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(JsonIgnoreAttribute).Assembly.Location),
                MetadataReference.CreateFromFile(Path.Combine(localPath, "Microsoft.Bcl.AsyncInterfaces.dll")),
            };
            string sourceCode;

            using (var fileStream = new FileStream(sourceCodeFile, FileMode.Open, FileAccess.Read))
                using (var reader = new StreamReader(fileStream))
                {
                    sourceCode = await reader.ReadToEndAsync();
                }
            if (!CSharpLanguage.CompileToAssembly(sourceCode, Path.GetFileNameWithoutExtension(assymblyFileName), assymblyFileName, references, out var diagnostics))
            {
                return(new CodeGenerationResult
                {
                    Succeeded = false,
                    SourceCodeFileName = sourceCodeFile,
                    Diagnostics = diagnostics?.Select(x => new DiagnosticInfo {
                        Message = x.GetMessage()
                    })
                });
            }
            return(new CodeGenerationResult
            {
                Succeeded = true,
                AssemblyFileName = assymblyFileName,
                SourceCodeFileName = sourceCodeFile
            });
        }
Пример #3
0
        public static bool CompileToAssembly(string code, string assemblyName, string path, IEnumerable <MetadataReference> references
                                             , out IEnumerable <Diagnostic> diagnostics)
        {
            var         sourceLanguage = new CSharpLanguage();
            SyntaxTree  syntaxTree     = sourceLanguage.ParseText(code, SourceCodeKind.Regular);
            Compilation compilation    = sourceLanguage
                                         .CreateLibraryCompilation(assemblyName: assemblyName, enableOptimisations: false, references)
                                         .AddSyntaxTrees(syntaxTree);

            using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                var emitResult = compilation.Emit(stream);
                if (!emitResult.Success)
                {
                    diagnostics = emitResult.Diagnostics;
                    return(false);
                }
                stream.Flush();
            }
            diagnostics = null;
            return(true);
        }