我正在寻找使用 xaml 文件来制作自动生成文件(*.g.cs 文件)
我在 roslyn 解决方案的 MSBuidWorkspaceTests.cs 中找到了这种方法:
public void TestOpenProjectAsyncWithXaml()
{
CreateFiles(GetSimpleCSharpSolutionFiles()
.WithFile(@"CSharpProject\CSharpProject.csproj", GetResourceText("CSharpProject_CSharpProject_WithXaml.csproj"))
.WithFile(@"CSharpProject\App.xaml", GetResourceText("CSharpProject_App.xaml"))
.WithFile(@"CSharpProject\App.xaml.cs", GetResourceText("CSharpProject_App.xaml.cs"))
.WithFile(@"CSharpProject\MainWindow.xaml", GetResourceText("CSharpProject_MainWindow.xaml"))
.WithFile(@"CSharpProject\MainWindow.xaml.cs", GetResourceText("CSharpProject_MainWindow.xaml.cs")));
var project = MSBuildWorkspace.Create().OpenProjectAsync(GetSolutionFileName(@"CSharpProject\CSharpProject.csproj")).Result;
var documents = project.Documents.ToList();
// AssemblyInfo.cs, App.xaml.cs, MainWindow.xaml.cs, App.g.cs, MainWindow.g.cs, + unusual AssemblyAttributes.cs
Assert.Equal(6, documents.Count);
// both xaml code behind files are documents
Assert.Equal(true, documents.Contains(d => d.Name == "App.xaml.cs"));
Assert.Equal(true, documents.Contains(d => d.Name == "MainWindow.xaml.cs"));
// prove no xaml files are documents
Assert.Equal(false, documents.Contains(d => d.Name.EndsWith(".xaml")));
// prove that generated source files …Run Code Online (Sandbox Code Playgroud) 我正在寻找从模块的 roslyn 语义模型中获取祖先。
在这样的课程中:
namespace Name1.Name2
{
using System;
...
public partial class MyClass : Ancestor<Param1, Param2>
{
}
}
Run Code Online (Sandbox Code Playgroud)
所以我试图得到 Ancestor<Param1, Param2>(以及后来的Param1和Param2)。
我使用此代码来创建语义模型:
SyntaxTree tree = CSharpSyntaxTree.ParseFile(moduleAutoGenPath);
CompilationUnitSyntax root = (CompilationUnitSyntax)tree.GetRoot();
var nameSpace = ((NamespaceDeclarationSyntax)(root.Members[0])).Name.ToString();
var compilation = CSharpCompilation.Create(nameSpace, new[] { tree }).AddReferences(new MetadataFileReference(typeof(object).Assembly.Location));
Run Code Online (Sandbox Code Playgroud)
我正在寻找,compilation.Assembly.Modules但没有找到祖先。
我的路还好吗?还是完全迷失了?