您需要将全局类型添加到设计时编译中。为此,您需要一个自定义RoslynHost
(在 2.4 及更高版本中支持):
public class CustomRoslynHost : RoslynHost
{
protected override Project CreateProject(Solution solution, DocumentCreationArgs args, CompilationOptions compilationOptions, Project previousProject = null)
{
var name = args.Name ?? "Program";
var id = ProjectId.CreateNewId(name);
var parseOptions = new CSharpParseOptions(kind: SourceCodeKind.Script, languageVersion: LanguageVersion.Latest);
compilationOptions = compilationOptions.WithScriptClassName(name);
solution = solution.AddProject(ProjectInfo.Create(
id,
VersionStamp.Create(),
name,
name,
LanguageNames.CSharp,
isSubmission: true,
parseOptions: parseOptions,
hostObjectType: typeof(MyContext),
compilationOptions: compilationOptions,
metadataReferences: previousProject != null ? ImmutableArray<MetadataReference>.Empty : DefaultReferences,
projectReferences: previousProject != null ? new[] { new ProjectReference(previousProject.Id) } : null));
var project = solution.GetProject(id);
return project;
}
}
然后添加对该类型所在的程序集的引用。例如:
new CutomRoslynHost(
references: RoslynHostReferences.Default.With(
typeNamespaceImports: new[] { typeof(MyContext) }),
additionalAssemblies: ...);