roslyn

What's the right way to update Roslyn's Document while typing?

血红的双手。 提交于 2019-12-10 11:26:10
问题 What's the right way to update Roslyn's Document when user is typing new text? Should I call SourceText.WithChanges and then Document.WithText on each char, or is there a more efficient way? Unfortunately throttling is not an answer, see my question What's the most efficient way to use Roslyn's CompletionSevice when typing? where an API implies that SourceText / Document must be up to date on each char. 回答1: Doing SourceText.WithChanges on each character is probably your best bet, assuming

What's the most efficient way to build up a IDocument from the very start

瘦欲@ 提交于 2019-12-10 11:08:55
问题 I would like to build up a new IDocument object step by step using the following class as a specific example. You can start with any object you like and use any intermediate objects you like as long as the resulting object is an IDocument which represents the complete class at the end. Step #1: Add a new namespace called MyNamespace. Printing out the current object should look like this at this point: namespace MyNamespace { } Step #2: Add a new class to this namespace called MyClass.

`Add-Type` C# 6+ features throwing errors

耗尽温柔 提交于 2019-12-10 10:17:44
问题 I am trying to compile c# source code in powershell by using this command: Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $source But c#-6.0 features are not working, for example: Add-Type : c:\Users\...\AppData\Local\Temp\2\d2q5hn5b.0.cs(101) : Unexpected character '$' For code: new Problem($"... ({identifier})", node) I am using powershell-v5.1 Is there a way to fix this? 回答1: Powershell uses CodeDomProvider to compile their assemblies. The version provided with the framework

Why ScriptEngine has not Execute method anymore?

家住魔仙堡 提交于 2019-12-10 10:17:41
问题 Currently I've started to work with Roslyn more seriously. I found this article about using ScriptEngine on Eric Vogel's blog. Due his blog, there should be an Execute method in this class: scriptEngine.Execute("1+1"); But it seems it doesn't exist anymore. Where does it gone? 回答1: It was moved to the session object. First you need to var session = engine.CreateSession(SomeHostObject); session.Execute("some code"); Though it should be noted Roslyn has been released as Microsoft.CodeAnalysis

Retrieve all Types with Roslyn within a solution

徘徊边缘 提交于 2019-12-10 09:46:47
问题 Does anyone know how to retrieve all available types (the semantic) within a solution? The creation of the compilation out of several projects is easy. MSBuildWorkspace workspace = MSBuildWorkspace.Create(); var solution = await workspace.OpenSolutionAsync(solutionPath, cancellationToken); var compilations = await Task.WhenAll(solution.Projects.Select(x => x.GetCompilationAsync(cancellationToken))); Just iterating over all ClassDeclarations is not enough for me because I want all types and

Get semantic model from a classifier VSIX

被刻印的时光 ゝ 提交于 2019-12-10 09:44:02
问题 As R# is not supporting the Roslyn Early Preview C# 6.0 features, the code looks very dull... I would like to colorize the code using a classifier VSIX. Is it possible to get the semantic model from the Roslyn Language Service for the current document? 回答1: You need to add a reference to Microsoft.CodeAnalysis.EditorFeatures.Text.dll, then use the following code. var doc = point.Snapshot.GetOpenDocumentInCurrentContextWithChanges(); var model = await doc.GetSemanticModelAsync(); This requires

Splitting the Expression statements with Roslyn

◇◆丶佛笑我妖孽 提交于 2019-12-10 09:36:38
问题 I am working in developing an application that rephrases CSharp code. I am doing this with Roslyn . I am facing a problem with splitting expressions . Sample class class Program { static void Main(string[] args) { float floatVariable = 20; Int16 intVariable = 10; string str = "School"; Console.Write(str + floatVariable.ToString() + intVariable.ToString()); // Facing problem with this statement } } Sample code I am using string code = new StreamReader(classPath).ReadToEnd(); var syntaxTree =

Applying multiple changes to a solution in roslyn

倖福魔咒の 提交于 2019-12-10 09:11:15
问题 I want to apply changes to several documents of a solution but only the first change is reflected and rest of them are rejected.This link shows how only once can changes be applied to a solution. What would be a work around for this. I would appreciate a link directing to a solution or a code snippet. Here is my function : public static async Task<bool> AddMethod(string solutionPath) { var workspace = MSBuildWorkspace.Create(); var solution = await workspace.OpenSolutionAsync(solutionPath);

How to turn off Visual Studio 2015 Roslyn error highlighting?

落花浮王杯 提交于 2019-12-10 07:52:38
问题 For Resharper we can selectively turn it on/off, but I could not find a way to turn off the built-in Visual Studio 2015 code highlights/suggestions. The reason I want to do this is because I have quite a few left-join linqs and these queries messed up Visual Studio. I am seeing lots of squiggles on my linqs and I prefer to turn them off. Here is one of the error I am talking about.. I have no idea how to get rid of it. The result of the expression is always 'true' since a value of type 'int'

Correct way to check the type of an expression in Roslyn analyzer?

女生的网名这么多〃 提交于 2019-12-10 01:29:02
问题 I'm writing a code analyzer with Roslyn, and I need to check if an ExpressionSyntax is of type Task or Task<T> . So far I have this: private static bool IsTask(ExpressionSyntax expression, SyntaxNodeAnalysisContext context) { var type = context.SemanticModel.GetTypeInfo(expression).Type; if (type == null) return false; if (type.Equals(context.SemanticModel.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task"))) return true; if (type.Equals(context.SemanticModel.Compilation