C# is not supported when I compile the code using Microsoft-CodeAnalysis. I wonder why
List<MetadataReference> refs = new List<MetadataReference>();
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var item in assemblies)
{
if (item.Location != null && item.Location != string.Empty)
{
refs.Add(MetadataReference.CreateFromFile(item.Location));
}
}
CSharpCompilationOptions compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
usings: null,
optimizationLevel: OptimizationLevel.Debug,
checkOverflow: false,
allowUnsafe: true,
platform: Platform.AnyCpu,
warningLevel: 4,
xmlReferenceResolver: null);
var adhoc = new AdhocWorkspace();
var solutionInfo = SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Default);
adhoc.AddSolution(solutionInfo);
var projectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Default, "RRQM", "RRQM", "C#")
.WithMetadataReferences(refs)
.WithCompilationOptions(compilationOptions);
adhoc.AddProject(projectInfo);
StringBuilder codeString = new StringBuilder();
codeString.AppendLine("using System.Reflection;");
codeString.AppendLine("using System.Runtime.CompilerServices;");
codeString.AppendLine("using System.Runtime.InteropServices;");
codeString.AppendLine("[assembly: ComVisible(false)]");
adhoc.AddDocument(projectInfo.Id, "AssemblyInfo.cs", SourceText.From(codeString.ToString()));
Project project = adhoc.CurrentSolution.GetProject(projectInfo.Id);
var compilation = project.GetCompilationAsync().Result;
using (MemoryStream dllStream = new MemoryStream())
using (Stream win32resStream = compilation.CreateDefaultWin32Resources(
versionResource: true, // Important!
noManifest: false,
manifestContents: null,
iconInIcoFormat: null))
{
EmitResult result = compilation.Emit(dllStream, win32Resources: win32resStream);
if (!result.Success)
{
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
diagnostic.IsWarningAsError ||
diagnostic.Severity == DiagnosticSeverity.Error);
StringBuilder sboutputMessage = new StringBuilder();
foreach (Diagnostic diagnostic in failures)
{
sboutputMessage.AppendFormat("\t{0} {1}: {2}", diagnostic.Location.ToString(), diagnostic.Id, diagnostic.GetMessage() + Environment.NewLine);//调试的最终输出信息
}
}
else
{
}
}