gpt4 book ai didi

c# - 使用 Roslyn 编译

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:21 26 4
gpt4 key购买 nike

我正在尝试编译解决方案,但在尝试保存时收到一条模糊的错误消息。

Anonymous type <>__blahblah not completed

但是在编译的源代码中没有任何匿名类型。

代码是

private static void Compile(ISolution solution)
{
var workspaceServices = (IHaveWorkspaceServices)solution;
var projectDependencyService = workspaceServices.WorkspaceServices.GetService<IProjectDependencyService>();

foreach (var projectId in projectDependencyService.GetDependencyGraph(solution).GetTopologicallySortedProjects())
{
var currentDomain = AppDomain.CurrentDomain;
var assemblyName = new AssemblyName();
assemblyName.Name = "Most." + solution.GetProject(projectId).AssemblyName;

var assemblyBuilder = currentDomain.DefineDynamicAssembly
(assemblyName, AssemblyBuilderAccess.RunAndSave);

var moduleBuilder = assemblyBuilder.
DefineDynamicModule(assemblyName.Name);
var types = moduleBuilder.GetTypes();
types.Aggregate((ts, t) =>
{
var tb = t as TypeBuilder;
return tb.CreateType();
});
solution.GetProject(projectId).GetCompilation().Emit(moduleBuilder);
assemblyBuilder.Save(assemblyName.Name);
}
}

最佳答案

这是 Roslyn 中的一个错误。转载于:

var assemblyName = new AssemblyName { Name = "Test" };

var compilation = Compilation.Create(
assemblyName.Name, new CompilationOptions(OutputKind.DynamicallyLinkedLibrary),
new[] {
SyntaxTree.ParseText(@"
using System;
using System.Linq;

public class C1
{
public void M1()
{
new[] {1}.Select(_ => new {});
}
}")
},
new[] {
MetadataReference.CreateAssemblyReference("mscorlib"),
MetadataReference.CreateAssemblyReference("System.Core")
});

var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
assemblyName, AssemblyBuilderAccess.RunAndSave);
var moduleBuilder = assembly.DefineDynamicModule(assemblyName.Name, assemblyName.Name + ".dll");

var emitResult = compilation.Emit(moduleBuilder);

if (emitResult.Success)
{
assembly.Save(assemblyName.Name + ".dll"); //Error! Type '<>f__AnonymousType0' was not completed.
}
else
{
throw new NotImplementedException();
}

关于c# - 使用 Roslyn 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18251968/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com