roslyn

Why are async state machines classes (and not structs) in Roslyn?

放肆的年华 提交于 2019-11-29 20:47:57
Let’s consider this very simple async method: static async Task myMethodAsync() { await Task.Delay(500); } When I compile this with VS2013 (pre Roslyn compiler) the generated state-machine is a struct. private struct <myMethodAsync>d__0 : IAsyncStateMachine { ... void IAsyncStateMachine.MoveNext() { ... } } When I compile it with VS2015 (Roslyn) the generated code is this: private sealed class <myMethodAsync>d__1 : IAsyncStateMachine { ... void IAsyncStateMachine.MoveNext() { ... } } As you can see Roslyn generates a class (and not a struct). If I remember correctly the first implementations

Sort and remove (unused) using statements Roslyn script/code?

北城以北 提交于 2019-11-29 18:50:02
问题 Sort and remove (unused) using statements Roslyn script/code? I'm looking for some .NET/Roslyn (compiler as service) code that can run through a project and sort and remove unused using statements. I believe this is possible with Roslyn? Can anyone point me to code that could do this rewrite? 回答1: This is a feature in Visual Studio, but academically I think you would collect using statements from your SyntaxTree like this: var usings = syntaxTree.Root.DescendentNodes().Where(node is

How do I disable all Roslyn Code Analyzers?

左心房为你撑大大i 提交于 2019-11-29 16:50:54
问题 I'm trying to work with a large opensource project that has a handful of Roslyn Code Analyzers. When I open the solution Visual Studio uses ~35% CPU for about 15 minutes. Using PerfView I've figured out that the code analyzers being run on the solution are bogging down Visual Studio. I know it's possible to disable analyzers on a per-project basis but this solution contains over 100 projects so I'd rather not do this one-by-one. My question(s): Can I disable all Roslyn Analyzers for a given

Roslyn Code Action: How to check if preview or real execution?

前提是你 提交于 2019-11-29 16:18:15
I am currently experimenting with Roslyn and Code Actions, more specific Code Refactorings. It feels kind of easy, but I have a difficulty I cannot solve. Code actions are executed once against a dummy workspace as a "preview" option, so that you can see the actual changes before you click the action and execute it against the real workspace. Now I am dealing with some things Roslyn can't really do (yet), so I am doing some changes via EnvDTE . I know, it's bad, but I couldn't find another way. So the issue here is: When I hover over my code action, the code gets executed as preview, and it

Loading an assembly generated by the Roslyn compiler

别来无恙 提交于 2019-11-29 14:36:38
问题 I'm generating a Greeter.dll using the Roslyn compiler. My problem occurs trying to load the DLL file. Here's the code: using System; using Roslyn.Compilers; using Roslyn.Compilers.CSharp; using System.IO; using System.Reflection; using System.Linq; namespace LoadingAClass { class Program { static void Main(string[] args) { var syntaxTree = SyntaxTree.ParseCompilationUnit(@" class Greeter { static void Greet() { Console.WriteLine(""Hello, World""); } }"); var compilation = Compilation.Create(

Roslyn Get Method Declaration from InvocationExpression

僤鯓⒐⒋嵵緔 提交于 2019-11-29 12:13:41
I'm making a roslyn demo for generating compiler warnings from attributes I have an analyzer to analyze Method Invocations which looks like so: public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction(AnalyzerInvocation, SyntaxKind.InvocationExpression); } private static void AnalyzerInvocation(SyntaxNodeAnalysisContext context) { var invocation = (InvocationExpressionSyntax)context.Node; } I'm trying to figure out how to get the method declaration, I know I can use the SymbolFinder to search for the method declaration var model = compilation.GetSemanticModel

Where has the Code Analysis window gone?

白昼怎懂夜的黑 提交于 2019-11-29 10:29:51
问题 In Visual Studio 2013, I used the Code Analysis window to provide reports to both the Development and Management teams. In Visual Studio 2015 Enterprise RTM, these errors have returned to the error window and I can no longer just see CA issues for a single project, or filter them by type. Is there a way to bring the Code Analysis window back? 回答1: Visual Studio Code Analysis has been merged with Intellisense and can now be viewed from the Error window. When you select "Build+Intellisense" or

Roslyn and .NET Runtime version [duplicate]

若如初见. 提交于 2019-11-29 09:08:59
This question already has an answer here: Does C# 6.0 work for .NET 4.0? 5 answers Is it possible to use Roslyn compiler and new features of C# 6.0 with old versions of .NET Runtime (for example, .NET 4.0)? For example, I want use the expression-bodied members ( int S => x + y; instead of int S { get { return x + y; } } ) in .NET 4.0 application. The new C# 6.0 features don't depend upon framework support so yes, you an app compiled with the C# 6.0 compiler will run on .NET 4.0, assuming of course you specify you are targeting .NET 4.0 in your project file in the first place. As of now Roslyn

Run F# code in C# - (Like C# in C# using Roslyn)

无人久伴 提交于 2019-11-29 07:24:12
问题 Is there a way to run F# code in C#? I have written an app in C# - I'd like to provide it the ability to execute F#, read Record objects, enumerate lists from F#. What is the current solution for this? I know in the future there will be probably be a way to do this via an update to Roslyn. Also, curious how to run F# code in F#, currently. Is there a way to do that easily? 回答1: Currently, you have to make the F# a library, and then call it from C#. Since F# is just .NET, using the F# library

How to add white space and/or format code?

左心房为你撑大大i 提交于 2019-11-29 06:06:27
Given that I have created a symbol using SymbolFactory.CreateProperty, how would I add white space. Currently I get accessibility, modifiers, name, type etc. all stringed together when writing out document. Maybe I am writing it out wrong, or I need to add extra step to add white space? I use document.GetText() to write it out to console. Nope, that's what you expect. Generated nodes don't have whitespace, with the intent you'll process it once you're done. There are two options: Call .NormalizeWhitespace() on the nodes. This is an aggressive formatter that is really only useful if you're