roslyn

How to read XML documentation comments using Roslyn

ぃ、小莉子 提交于 2019-11-28 19:20:47
I would like to be able to read XML documentation comments while parsing C# source code using Roslyn. /// <summary> /// Documentation... /// </summary> I tried setting the ParseDocumentationComments in the ParseOptions, but it doesn't seem to have an effect? var parseOptions = ParseOptions.Default.WithParseDocumentationComments(true); SyntaxTree unit = SyntaxTree.ParseFile(file, parseOptions); You'll need to either: Look at the LeadingTrivia of the syntax that contains the XML doc comments Construct a Compilation , find the Symbol that has the XML doc comment and use the

Using System.Dynamic in Roslyn

我的梦境 提交于 2019-11-28 19:02:44
I modified the example that comes with the new version of Roslyn that was released yesterday to use dynamic and ExpandoObject but I am getting a compiler error which I am not sure how to fix. The error is: (7,21): error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create' Can you not use dynamics in the new compiler yet? How can I fix this? Here is the example that I updated: [TestMethod] public void EndToEndCompileAndRun() { var text = @"using System.Dynamic; public class Calculator { public static object Evaluate() { dynamic x = new

How do I compile a C# solution with Roslyn?

时间秒杀一切 提交于 2019-11-28 17:32:55
I have a piece of software that generates code for a C# project based on user actions. I would like to create a GUI to automatically compile the solution so I don't have to load up Visual Studio just to trigger a recompile. I've been looking for a chance to play with Roslyn a bit and decided to try and use Roslyn instead of msbuild to do this. Unfortunately, I can't seem to find any good resources on using Roslyn in this fashion. Can anyone point me in the right direction? You can load the solution by using Roslyn.Services.Workspace.LoadSolution . Once you have done so, you need to go through

Building a SyntaxTree from the ground up

a 夏天 提交于 2019-11-28 17:16:00
I previously asked this question, which was answered, but someone gave a suggestion that might help me prevent making similar mistakes as I move forward. Adding Auto-Implemented Property to class using Roslyn The suggestion was that I build the Syntax Tree from the bottom up and not from the top down. Could someone provide a small demo or a link that shows how I would do this from the ground up? Here is the code again: var root = (CompilationUnitSyntax)document.GetSyntaxRoot(); // Add the namespace var namespaceAnnotation = new SyntaxAnnotation(); root = root.WithMembers( Syntax

Difference in CSC and Roslyn compiler's static lambda expression evaluation?

99封情书 提交于 2019-11-28 13:40:40
Consider the following example code. class Program { static void Main( string[] args ) { DoSomethingWithAction( i => { Console.WriteLine( "Value: {0}", i ); } ); Console.ReadLine(); } private static void DoSomethingWithAction( Action<int> something ) { Console.WriteLine( something.Target == null ? "Method is static." : "Method is not static." ); something( 5 ); } } If I compile and run this code under Debug using the Visual Studio 2010 (under CSC compiler) it will print out the following result: Method is not static. Value: 5 If I compile the same code in Visual Studio 2010, but this time

Roslyn 入门:使用 Roslyn 静态分析现有项目中的代码

微笑、不失礼 提交于 2019-11-28 12:23:10
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接: https://blog.csdn.net/WPwalter/article/details/79616402 Roslyn 是微软为 C# 设计的一套分析器,它具有很强的扩展性。以至于我们只需要编写很少量的代码便能够分析我们的项目文件。 作为 Roslyn 入门篇文章,你将可以通过本文学习如何开始编写一个 Roslyn 扩展项目,如何开始分析一个解决方案(.sln)中项目(.csproj)的代码文件(.cs)。 本文是 Roslyn 入门系列之一: Roslyn 入门:使用 Visual Studio 的语法可视化(Syntax Visualizer)窗格查看和了解代码的语法树 Roslyn 入门:使用 .NET Core 版本的 Roslyn 编译并执行跨平台的静态的源码 Roslyn 入门:使用 Roslyn 静态分析现有项目中的代码(本文) 如果你希望真实地静态分析一个实际项目,并且理解这样的分析过程是如何进行的(而不只是写个 demo),那么本文的所有内容都将是必要的。 准备工作 为了能够进行后面关键的操作,我们需要先有一个能跑起来的项目。 ▲ 在 Visual Studio 新建项目,选择“控制台程序(.NET Framework)” 在目前({%

How to get reference to 'Roslyn' Workspace object from IVsSolution?

萝らか妹 提交于 2019-11-28 11:39:18
I have a VS Package project from which I need to access Roslyn or Microsoft.CodeAnalysis' Workspace OR Solution object from the loaded IVsSolution . I need to know how I could achieve that ? I found this stackoverflow discussion here which suggests to use PrimaryWorkspace static property of Workspace class which I can't find in Microsoft.CodeAnalysis.Workspace EDIT: I found out that Microsoft.CodeAnalysis does not have this yet but I downloaded the older release of Roslyn from Nuget.org which has this. But now PrimaryWorkspace Property is giving me NULL :( I am using Isolated Shell. Kevin

Finding all class declarations than inherit from another with Roslyn

拥有回忆 提交于 2019-11-28 11:28:45
I have a CSharpCompilation instance containing an array of SyntaxTree s and I am trying to find all the class declarations that inherit from a class e.g // Not in syntax tree but referenced in project public class Base{} // In syntax tree, how to find all such classes? public class MyClass : Base {} I've tried a few things but am a bit confused with all the options and can't seem to find the right way to do this. I've tried to get the symbols but this doesn't work for inherited types SyntaxTree[] trees = context.CSharpCompilation.SyntaxTrees; IEnumerable<ISymbol> symbols = context

Can Roslyn generate source code from an object instance?

北慕城南 提交于 2019-11-28 09:29:56
Using the Roslyn API with Visual Studio 2015, can I convert an object instance to source code? Can I create an extension method like ".ToSourceCode()" as shown below? class Foo { } class Program { static string classSourceCode = "class Foo { }"; static void Main() { var instance = new Foo(); var instanceSourceCode = instance.GetType().ToSourceCode(); System.Diagnostics.Debug.Assert(instanceSourceCode == classSourceCode); } } No. However, ILSpy can. Based on the comments on the question and what I understand about Roslyn, decompilation is not supported. However, thanks to @Bradley's ILSpy tip,

Get fully-qualified metadata name in Roslyn

扶醉桌前 提交于 2019-11-28 09:11:10
I need to get the full CLR name of a particular symbol. This means that for generic types I need the `1 , `2 , etc. appended to types. Now, ISymbol already has a property MetadataName which does exactly that. But it excludes surrounding types and namespaces, only giving the name of the symbol at hand. The usual option for getting a fully-qualified name, i.e. via ToDisplayString doesn't quite work here because it will not use the MetadataName for its various parts. Is there anything like this built-in? Or do I have to just concatenate the chain of ContainingSymbol s with . in between? (And are