使用Roslyn解析类函数和属性

本文关键字:属性 类函数 Roslyn 使用 | 更新日期: 2023-09-27 18:00:59

我是Roslyn的新手,我正在尝试解析cs文件并获取函数和属性。

API示例

//won't work any more
var tree = SyntaxTree.ParseText(...); 
var tree = SyntaxTree.ParseFle(...);
var root = tree.Root; 

API似乎已经改变了,它将不再工作。我猜它与2012年罗斯林CTP仅从外观上看。

然后我尝试一个新的例子:

//won't work neither
var compilation = CSharpCompilation.Create("HelloWorld") 
                  .AddReferences( new MetadataFileReference( 
                  typeof(object).Assembly.Location)) .AddSyntaxTrees(tree);
var tree = CSharpSyntaxTree.ParseText(strSourceCode);
var root = tree.GetRoot(); 

同样,它将不再工作,因为MetadataFileReference是我得到的Roslyn版本中的一个抽象类。API再次更改?我不确定。

VS&包裹

我正在使用VS2013 PRO UPDATE 2从我的公司pc。2015年7月5日至今。不,我没有带VS2015的电脑,我不确定这是否重要。

我打开了一个新的WinForm项目,然后在NuGet PM中,我按照https://github.com/dotnet/roslyn.这是我的安装日志:

PM> Install-Package Microsoft.CodeAnalysis -Pre
Attempting to resolve dependency 'Microsoft.CodeAnalysis.CSharp.Workspaces (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.CSharp (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.Common (= 1.0.0-rc3)'.
Attempting to resolve dependency 'System.Collections.Immutable (≥ 1.1.36)'.
Attempting to resolve dependency 'System.Reflection.Metadata (≥ 1.0.21)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.Analyzers (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.Workspaces.Common (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.Composition (≥ 1.0.27)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.VisualBasic.Workspaces (= 1.0.0-rc3)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.VisualBasic (= 1.0.0-rc3)'.
Installing 'System.Collections.Immutable 1.1.36'.
You are downloading System.Collections.Immutable from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=329770. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'System.Collections.Immutable 1.1.36'.
Installing 'System.Reflection.Metadata 1.0.21'.
You are downloading System.Reflection.Metadata from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=329770. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'System.Reflection.Metadata 1.0.21'.
Installing 'Microsoft.CodeAnalysis.Analyzers 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.Analyzers from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.Analyzers 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.Common 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.Common from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.Common 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.CSharp 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.CSharp from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.CSharp 1.0.0-rc3'.
Installing 'Microsoft.Composition 1.0.27'.
You are downloading Microsoft.Composition from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=329770. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.Composition 1.0.27'.
Installing 'Microsoft.CodeAnalysis.Workspaces.Common 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.Workspaces.Common from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.Workspaces.Common 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.CSharp.Workspaces 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.CSharp.Workspaces from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.CSharp.Workspaces 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.VisualBasic 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.VisualBasic from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.VisualBasic 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis.VisualBasic.Workspaces from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 1.0.0-rc3'.
Installing 'Microsoft.CodeAnalysis 1.0.0-rc3'.
You are downloading Microsoft.CodeAnalysis from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=529443. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis 1.0.0-rc3'.
Adding 'System.Collections.Immutable 1.1.36' to MyWinForm.
Successfully added 'System.Collections.Immutable 1.1.36' to MyWinForm.
Adding 'System.Reflection.Metadata 1.0.21' to MyWinForm.
Successfully added 'System.Reflection.Metadata 1.0.21' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.Analyzers 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.Analyzers 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.Common 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.Common 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.CSharp 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.CSharp 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.Composition 1.0.27' to MyWinForm.
Successfully added 'Microsoft.Composition 1.0.27' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.Workspaces.Common 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.Workspaces.Common 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.CSharp.Workspaces 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.CSharp.Workspaces 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.VisualBasic 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.VisualBasic 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 1.0.0-rc3' to MyWinForm.
Adding 'Microsoft.CodeAnalysis 1.0.0-rc3' to MyWinForm.
Successfully added 'Microsoft.CodeAnalysis 1.0.0-rc3' to MyWinForm.

我的目标

我只是想:

  1. 将cs文件或某些源字符串读取到编译器中
  2. 解析出类名和函数属性名
  3. 解析出函数和属性及其参数和返回类型

我可以举一些例子吗?

使用Roslyn解析类函数和属性

这里有几件事:

强烈建议您将Visual Studio 2015用于Roslyn相关任务。Roslyn的某些部分依赖于VS 2015和最新的MSBuild工具。Visual Studio 2015社区也是免费的!

注意:我还建议使用NuGet中的rc-2位,而不是rc-3位。你在一个奇怪的时间(在两次发布之间(进入了罗斯林世界,这应该会让你的生活变得更轻松,直到VS2015于7月20日发布。

使用以下内容从NuGet安装:

Install-Package Microsoft.CodeAnalysis -Version 1.0.0-rc2 -Pre

在以下情况下使用CreateFromAssembly

var tree = CSharpSyntaxTree.ParseText(@"
class TestClass 
{
    void TestMethod ()
    {
        int i;
    }
}");
var Mscorlib = MetadataReference.CreateFromAssembly(typeof(object).Assembly);
var compilation = CSharpCompilation.Create("MyCompilation",
    syntaxTrees: new[] { tree }, references: new[] { Mscorlib });
var model = compilation.GetSemanticModel(tree);