c# - 为 roslyn 脚本项目设置基类
问题描述
所以我有一个编译器类,用于运行用户代码。现在我的问题是我的文档是脚本文档,我不知道如何为这些文档设置基类。我一直在寻找 hostObjectType 但我看到 isSubmission 必须是真的,但我不能这样做,因为我的其余代码会中断。
初始化编译器的代码:
public static async Task Init(Type host, params Assembly[] assemblies)
{
await Task.Run(() =>
{
IsInitialized = true;
workspace = new AdhocWorkspace();
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true, usings: new string[] { "CCreative" }, platform: Platform.X64)
.WithMetadataImportOptions(MetadataImportOptions.All);
var scriptProjectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(), "Script", "Script", LanguageNames.CSharp, isSubmission: false, hostObjectType: host)
.WithMetadataReferences(assemblies.Select(x => MetadataReference.CreateFromFile(x.Location, default, x.Location.Contains("CCreative") ? new CCreativeDocumentationProvider() : null)))
.WithCompilationOptions(compilationOptions);
project = workspace.AddProject(scriptProjectInfo);
workspace.TryApplyChanges(project.Solution.WithOptions(GetOptions(workspace)));
});
}
所以我的目标是将所有脚本文档的基类设置为主机参数。
创建文档的代码:
public static DocumentId AddDocument(string text)
{
var scriptDocumentInfo = DocumentInfo.Create(DocumentId.CreateNewId(project.Id),
"Script",
sourceCodeKind: SourceCodeKind.Script,
loader: TextLoader.From(TextAndVersion.Create(SourceText.From(text), VersionStamp.Create())));
var scriptDocument = workspace.AddDocument(scriptDocumentInfo);
project = scriptDocument.Project;
workspace.TryApplyChanges(project.Solution);
return scriptDocument.Id;
}
欢迎任何帮助:)
解决方案
推荐阅读
- ios - 找不到 IOS 12.5.4 (16H50) 的设备支持文件
- azure - 如何使用机器学习从给定值中找到键
- spring-boot - 带有 @Restcontoller 计时问题的 Spring 集成流程
- python - 我在字母之间而不是单词之间用逗号得到单词
- highcharts - Highchart-export-server 未在图表上正确显示绘图带
- c# - 为什么我的炸弹撞击半径和爆炸效果在不同的位置实例化?
- kubernetes - 无法在 tekton 管道中推送 AWS ECR 图像
- r - read_html() 没有在 R 中获取 NES 股票网站的 HTML
- react-native - 需要使用可重复使用的开关创建一个切换组件
- html - 如何让我的 html 搜索栏在 google 上搜索项目