roslyn

What are the benefits of Compiler as a Service

断了今生、忘了曾经 提交于 2019-12-13 12:59:41
问题 In Anders Hejlsberg's .NET 4.0 presentation he discussed in NET 5.0 ("or some future release") they are working on a "Compiler as a Service" model. Anders Hejlsberg's states: [source][1] "We want to open up our compiler so it becomes an API you can call to compile a piece of code and get back expression trees and/or IL. This enables a whole host of scenarios, such as application programmability, an interactive prompt, user-written refactorings, and domain specific languages that have little

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

冷暖自知 提交于 2019-12-13 12:04:55
问题 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. 回答1: If you add an analyzer as Nuget and check the content of your project, you'll see that only an <Analyzer Include="..." /

vNext can't see namespace in referenced package

寵の児 提交于 2019-12-13 08:39:34
问题 Program.cs using System; using RazorEngine; namespace ConsoleApp1 { public class Program { public static void Main(string[] args) { string template = "Hello @Model.Name! Welcome to Razor!"; string result = Razor.Parse(template, new { Name = "World" }); Console.WriteLine(result); Console.WriteLine("Press enter to exit."); Console.ReadLine(); } } } project.json { "version": "1.0.0-*", "dependencies": { "RazorEngine": "3.4.2" }, "commands": { "run": "run" }, "frameworks": { "aspnet50": {},

How to access and edit globals in Roslyn scripting environment?

本秂侑毒 提交于 2019-12-13 07:15:29
问题 I have an application in which I use the Roslyn scripting engine (namespace Microsoft.CodeAnalysis.Scripting ). What I have right now is this: public static async Task<object> Execute(string code, CommandEventArgs e) { if (_scriptState == null) { var options = ScriptOptions.Default; options .AddReferences(typeof (Type).Assembly, typeof (Console).Assembly, typeof (IEnumerable<>).Assembly, typeof (IQueryable).Assembly) .AddImports("System", "System.Numerics", "System.Text", "System.Linq",

Reading the default namespace through Roslyn API

≡放荡痞女 提交于 2019-12-13 04:49:52
问题 Is there a way to read the default namespace setting from the IProject interface or any other Roslyn interface? I know that I can parse the project's file but I think this should be possible using Roslyn API but I cannot find how to do that. Thanks in advance for information. 回答1: Unfortunately, Roslyn does not expose a way to do that at the moment, but I agree that it is something we will probably need eventually. 回答2: The library Microsoft.Build.Evaluation, which is, I believe, the

Where is my annotation?

寵の児 提交于 2019-12-13 04:46:00
问题 When I try to annotate class, property and method, and then try to retrieve annotated node, only the class one is returned. Why? Here is the code that annotates SyntaxAnnotation propertyAnnotation = null; SyntaxAnnotation classAnnotation = null; SyntaxAnnotation setMethodAnnotation = null; document = document .AnnotateClass(classDeclaration, out classAnnotation) .AnnotateProperty(propertyDeclaration, out propertyAnnotation) .AnnotateSetMethod(setMethodDeclaration, out setMethodAnnotation); I

Rewrite SyntaxNode to two SyntaxNodes

主宰稳场 提交于 2019-12-13 02:31:59
问题 I'm using Roslyn's CSharpSyntaxRewriter to rewrite the following: string myString = "Hello "; myString += "World!"; to: string myString = "Hello "; myString += "World!"; Log("myString", myString); My syntax rewriter overrides VisitAssignmentExpression as follows. public override SyntaxNode VisitAssignmentExpression(AssignmentExpressionSyntax node) { //Construct a new node without trailing trivia var newNode = node.WithoutTrailingTrivia(); InvocationExpressionSyntax invocation = //Build proper

Get Symbol from CatchDeclaration

六眼飞鱼酱① 提交于 2019-12-13 01:05:57
问题 How do I get the symbol info of the instance in a CatchDeclaration? Basically I want to get the symbol so that I can compare it later to see that a method was called on that symbol. Basically I have this: catch (Exception ex) {} and I want the SymbolInfo for "ex". I get the catch declaration with: var catchDeclaration = catchClause.DescendantNodes().OfType<CatchDeclarationSyntax>().FirstOrDefault(); But I seem to only be able to get the SyntaxToken from the declaration ( catchDeclaration

Can Roslyn be used for VB.NET scripting?

孤街醉人 提交于 2019-12-12 17:50:17
问题 I'm creating a script editor for my application, and I'd like to have it be able to run both C# and VB.NET scripts using Roslyn. I got this working with C# by using the CSharpScript class in the Microsoft.CodeAnalysis.CSharp.Scripting assembly/namespace, however there is no equivalent VisualBasicScript class that I can find, nor a Microsoft.CodeAnalysis.VisualBasic.Scripting assembly. I've scoured the web and can find nothing useful about running VB.NET scripts, and all the samples on the

How can I identify and analyze local variables and parameters with Roslyn code diagnostics?

∥☆過路亽.° 提交于 2019-12-12 15:22:12
问题 I'm confused which method to use on the AnalysisContext context object to have every function/method's local variables anazlyed: either RegisterSymbolAction() or RegisterSyntaxNodeAction() . It's likely RegisterSymbolAction() as per the sample Diagnostic with Code Fix (NuGet + VSIX) in the Roslyn SDK Project Templates.vsix. I'm debugging using a simple console app whose Main() has a handful of local variables of type string and int . I've tried these two, but neither will trigger any variable