roslyn

Get dependencies between classes in Roslyn

混江龙づ霸主 提交于 2019-12-04 09:55:33
问题 I am successfully getting dependencies between projects with Roslyn, and now I would like to get dependencies between classes, similar to the Code Map feature in Visual Studio Enterprise. Here is my code, the "?????" part is where I imagine I could get something. I am very new to the Roslyn API, though, and I don't know how to proceed from there on. Solution solution = MSBuildWorkspace.Create() .OpenSolutionAsync(Path.Combine(repoRootFolder, "MySolution.sln")) .Result; ProjectDependencyGraph

Roslyn SyntaxTree changes injection

五迷三道 提交于 2019-12-04 09:07:47
I wrote my class MonitorSyntaxRewriter, which inherits from CSharpSyntaxRewriter. With this class I change my SyntaxTree. But, how can I "inject" this modified synaxtree somewhere? I mean, I have some random project in Visual Studio, and on Build, I would like all the syntax trees go through this MonitorSyntaxRewriter. Is there some option for that? Or is there some other possible workaround? (creating new solution...). I just don't want my *.cs files being changed in the project. As far as I know you can't plug into the build process and rewrite the syntax trees before they're compiled and

Roslyn slow startup time

社会主义新天地 提交于 2019-12-04 07:56:50
I've noticed that the startup time for Roslyn parsing/compilation is a fairly significant one-time cost. EDIT: I am using the Roslyn CTP MSI (the assembly is in the GAC). Is this expected? Is there any workaround? Running the code below takes almost the same amount of time with 1 iteration (~3 seconds) as with 300 iterations (~3 seconds). [Test] public void Test() { var iters = 300; foreach (var i in Enumerable.Range(0, iters)) { // Parse the source file using Roslyn SyntaxTree syntaxTree = SyntaxTree.ParseText(@"public class Foo" + i + @" { public void Exec() { } }"); // Add all the

Getting node from line number in Roslyn

送分小仙女□ 提交于 2019-12-04 07:54:12
How can I get a SyntaxNode based on a line number? Else if its possible to get LineSpan of that line number then to node. You can get the span of a line from the document text. From there, you can find all nodes that intersect with the span of the line. This will return multiple syntax nodes, which you can then use your criteria to pull out the one you are looking for: static void Main(string[] args) { var code = @" using System; namespace ConsoleApplication1 { class TypeName { public int Add(int x, int y) { return x+y; } } }"; var st = SourceText.From(code); var sf = SyntaxFactory

Are Roslyn SyntaxNodes reused?

℡╲_俬逩灬. 提交于 2019-12-04 07:27:14
问题 I've been taking a look to Roslyn CTP and, while it solves a similar problem to the Expression tree API, both are immutable but Roslyn does so in a quite different way: Expression nodes have no reference to the parent node, are modified using a ExpressionVisitor and that's why big parts can be reused. Roslyn's SyntaxNode , on the other side, has a reference to its parent, so all the nodes effectively become a block that's impossible to re-use. Methods like Update , ReplaceNode , etc, are

Can Roslyn be used to generate dynamic method similar to DynamicMethod IL generation

拈花ヽ惹草 提交于 2019-12-04 06:16:52
I have been using DynamiMethod to generate the IL using method.GetILGenerator(); This works well but is of course very hard to use since you generally don't want to work with low level IL in a high level language like C#. Now since there is Roslyn I though I can use that instead. I have tried to figure out how to use Roslyn to do similar thing: generate a dynamic method and then create a delegate for it. The only way I was able to do that is to have full class like this SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(@" using System; namespace RoslynCompileSample { public class Writer {

How to add a custom code analyzer to a project without nuget or VSIX?

萝らか妹 提交于 2019-12-04 04:11:56
I want to write a custom code analyzer in Visual Studio 2015 for a C# ConsoleApplication. For this reason I don't want to create a seperate "Analyzer with Code Fix" project from template, because this requires to add this analyzer in my projects as nuget package. Is it possible, to add a analyzer reference manually? I would like to reference the analyzer without nuget. If you add an analyzer as Nuget and check the content of your project, you'll see that only an <Analyzer Include="..." /> item is added. You can do the same manually. Also, you can do this in the .csproj.user file as well, so

Create a Func<> with Roslyn

守給你的承諾、 提交于 2019-12-04 03:46:51
问题 Inspired by this and this article, I'm trying to create a dynamic function with Roslyn. However the mentioned sources are outdated or not complete and I'm not able to create a functional sample. My work so far: var code = @"Func<int, int> doStuffToInt = i => { var result = i; for (var y = i; y <= i * 2; y++) { result += y; } return result; };"; var se = new ScriptEngine(); var session = se.CreateSession(); session.AddReference(typeof(Program).Assembly); session.AddReference(typeof(Expression)

How delete all comments of Source code in my c# solution with roslyn?

旧时模样 提交于 2019-12-04 01:43:06
问题 I want to delete all comment in my source code in my C# solution with Roslyn. but How should i do that ? public void DeleteComment() { var code = File.ReadAllText("code.cs"); SyntaxTree tree = SyntaxFactory.ParseSyntaxTree(code); ///Delete Comments ? } 回答1: Just some extensions to @SLaks' answer. You need to extend CSharpSyntaxRewriter and override the VisitTrivia method. And here you'll need to check the Kind of the trivia. Depending on your needs, you should filter for single and multiline

Get TypeSyntax from ITypeSymbol

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 01:29:13
I'm experimenting a bit with the Roslyn-CTP. Currently I'm trying to replace var with the concrete type. var i=1; should become: int i=1; Figuring out the inferred type is easy. But since this part happens in the semantic model I get a ITypeSymbol . The replacement happens in the syntax model, so I need a TypeSyntax . Since I don't want a bloated name ( global::System.Int32 ), the conversion is context dependent ( using , nested types etc.). The Visual studio version that's part of Roslyn already has this functionality in its "Simplify type name" quickfix, but looking over the samples I couldn