roslyn

Ship Roslyn code refactoring as NuGet package

僤鯓⒐⒋嵵緔 提交于 2019-12-12 13:53:33
问题 When installing the .NET Compiler Platform SDK you get Visual Studio templates for creating an "Analyzer with Code Fix (NuGet + VSIX)" and a "Code Refactoring (VSIX)". Why isn't there a template for shipping a code refactoring with NuGet (like "Code Refactoring (NuGet + VSIX)")? Is it even possible to add a code refactoring provider from a NuGet package (by creating the NuGet package manually)? Doesn't it make sense to install a code refactoring provider on a project per project basis? EDIT:

How to cancel CSharpScript.RunAsync

坚强是说给别人听的谎言 提交于 2019-12-12 12:32:47
问题 How can I stop RunAsync? CancellatioTokenSource cts = new CancellationTokenSource(); //I thought that it's must work, but it don't var script = CSharpScript.Create(code: someCode); await script.RunAsync(cancelletionToken: cts.Token); void button_click() { cts.Cancel() } How else can I do this. And for why such methods need cancellationToken parameter? 回答1: You must do it before the await call, which blocks execution until yout get results, e.g.: var task = script.RunAsync(cancelletionToken:

How to compare type symbols (ITypeSymbol) from different projects in Roslyn?

左心房为你撑大大i 提交于 2019-12-12 12:28:41
问题 I have 2 test projects in solution. First project ("VDoc") declare VDocQuery type. Second project ("VDocQueryTest" ) calls VDocQuery constructor. I get 2 VDocQuery's ITypeSymbol's (one from each project), compare it, but get false result. Steps: 1. Get first ITypeSymbol (from VDoc project with SemanticModel.LookupNamespacesAndTypes() method). 2. Get second ITypeSymbol from VDocQueryTest project (from ObjectCreationExpressionSyntax.GetTypeInfo().Type) 3. Compare it with ITypeSymbol.Equals

How to check if a property is decorated with a custom attribute using Roslyn?

孤街醉人 提交于 2019-12-12 11:06:48
问题 I want to analyse a C# class using Roslyn and intend to do something when visited property has the specific attribute applied to it. How can I do this in the CSharpSyntaxWalker.VisitPropertyDeclaration method override? For example, in the following code block I want to know whether the Date property has the Validation attribute or not, and if so, whether IsJDate is true or false? [Validation(IsJDate=true)] public string Date {get; set;} Initializations: filesPath.ToList().ForEach(csFilePath =

Splitting the expressions with Roslyn

懵懂的女人 提交于 2019-12-12 09:21:19
问题 I am using Roslyn and I want to split the statement as below, string stringVariable = "string"; int intVariable = 10; Console.Write(stringVariable + string.Concat("string1","string2") + intVariable.ToString()) Console.Write() stringVariable string.Concat("string1","string2") intVariable.ToString() I have asked a question and got answer for splitting the expressions Splitting the Expression statements with Roslyn but this suggestion splits the string.Concat("string1", "string2") as below,

Roslyn slow startup time

淺唱寂寞╮ 提交于 2019-12-12 08:54:04
问题 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 =

Get the SyntaxNode given the linenumber in a SyntaxTree

独自空忆成欢 提交于 2019-12-12 08:22:54
问题 I want to get the SyntaxNode of a line given the location(lineNumber). The code below should be self-explanatory, but let me know of any questions. static void Main() { string codeSnippet = @"using System; class Program { static void Main(string[] args) { Console.WriteLine(""Hello, World!""); } }"; SyntaxTree tree = SyntaxTree.ParseCompilationUnit(codeSnippet); string[] lines = codeSnippet.Split('\n'); SyntaxNode node = GetNode(tree, 6); //How?? } static SyntaxNode GetNode(SyntaxTree tree,int

Roslyn Fix Provider Check if Fix is From Preview Window

放肆的年华 提交于 2019-12-12 05:27:06
问题 I have written a fix provider which adds members elements resx file. I noticed when visual studio generates the expected changes it calls the method in which add the resource keys to the file. I'm registering my FixProvider I'm looking for a way to tell if the fix was called from a preview public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); // TODO: Replace the

How to pause or stop “CSharpScript” running?

女生的网名这么多〃 提交于 2019-12-12 04:23:22
问题 I'm using Roslyn's scripting API in my application, code snippet as following: public class ScriptEngine { public static string CodeText; public static event Action<string[]> CompileErrorEvent; public static async Task<bool> RunScriptAsync(CancellationToken ct) { try { var scriptResult = await CSharpScript.RunAsync(CodeText, null, new ScriptHost(), null, ct); return true; } catch (Microsoft.CodeAnalysis.Scripting.CompilationErrorException ex) { List<string> result = new List<string>();

Roslyn - dynamic (runtime) flow

别说谁变了你拦得住时间么 提交于 2019-12-12 04:05:41
问题 I started playing with Roslyn. It’s relatively easy to parse code and do static analysis. I wonder if it’s possible to use it for runtime analysis? I want to call a method with parameters and check which branches were executed. In other words, I need a runtime execution plan. Is it something which could be done with Roslyn? 回答1: I don't know what the best solution is and I would defer to anything SLaks recommends in most cases. However... If you want to do this with Roslyn you certainly can.