roslyn

Checking a Variable Type For Code Analysis

佐手、 提交于 2019-12-07 06:46:43
问题 What is the correct way to check a variable's type in a Roslyn code analyzer? I am registering for a ObjectCreationExpressionSyntax node and I can get the type, but I am not certain the correct way to check that it is a type I care about. I found a way to do it by checking the display string, but I am wondering if there is a more correct way to accomplish this. For example, here is code that is trying to check for an ArrayList creation. private static void SyntaxValidator

Detect c# 6 features with Roslyn

非 Y 不嫁゛ 提交于 2019-12-07 06:07:09
问题 Is there a way to detect a c# 6 feature with a roslyn diagnostic analyzer? I have a solution that links in some files from a project that cannot use c#6 features, so I want to make that an error just for those files. To be clear - I cannot set the whole project to c#5, only some files are off limits. I can try and catch specific features, but it's cumbersome and was wondering if there is a faster way? 回答1: You can use this Walker for detecting C# 6 syntax features: public class

Using an AdHocWorkspace results in “The language 'C#' is not supported.”

被刻印的时光 ゝ 提交于 2019-12-07 05:39:00
问题 Using the RC2 of Microsoft.CodeAnalysis.CSharp.Workspaces in VS2015, this code throws an exception: var tree = CSharpSyntaxTree.ParseText(...); var workspace = new AdhocWorkspace(); var newRoot = Simplifier.Expand(tree.GetRoot(), compilation.GetSemanticModel(tree, false), workspace, n => true, true, CancellationToken.None); The exception message is "The language 'C#' is not supported." What am I missing to make this work? 回答1: You need to add a reference to the C# Workspaces NuGet package.

Project MetadataReferences is not populated when ProjectReferences are present

走远了吗. 提交于 2019-12-07 04:38:02
问题 I'm loading in a solution in the MSBuildWorkspace: var msWorkspace = MSBuildWorkspace.Create(); var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result; Projects without ProjectReferences show all MetadataReferences, including mscorlib. Projects with ProjectReferences have an empty collection of MetadataReferences. As compilation works, I guess the compiler platform for some reason is not populating this collection, and I'm wondering why? If I remove the ProjectReferences, the

Can Roslyn be installed without Visual Studio?

夙愿已清 提交于 2019-12-07 04:30:50
问题 The Roslyn end-user preview is a VSIX (Visual Studio extension), but it replaces the compilers in the system .NET Framework installation, such that involving csc.exe from the command-line will begin using Roslyn. Is it possible to install the Roslyn csc.exe on a computer without Visual Studio installed? How? (Yes, Roslyn works with Visual Studio 2013 Express, so licensing is not an issue. But disk space IS. Even the Express edition has a very large footprint compared to say SharpDevelop.) 回答1

StackExchange.Precompilation - How can I unit test precompilation diagnostics?

╄→гoц情女王★ 提交于 2019-12-07 03:29:15
问题 Background I'm using StackExchange.Precompilation to implement aspect-oriented programming in C#. See my repository on GitHub. The basic idea is that client code will be able to place custom attributes on members, and the precompiler will perform syntax transformations on any members with those attributes. A simple example is the NonNullAttribute I created. When NonNullAttribute is placed on a parameter p , the precompiler will insert if (Object.Equals(p, null)) throw new

How to access invocations through extension methods, methods in static classes and methods with ref/out parameters with Roslyn

梦想与她 提交于 2019-12-07 03:11:53
问题 I'm working on creating an open source project for creating .NET UML Sequence Diagrams that leverages a javascript library called js-sequence-diagrams. I am not sure Roslyn is the right tool for the job, but I thought I would give it a shot so I have put together some proof of concept code which attempts to get all methods and their invocations and then outputs these invocations in a form that can be interpreted by js-sequence-diagrams. The code generates some output, but it does not capture

How to use a Roslyn scripting submission as an assembly in other Roslyn compilations

ぃ、小莉子 提交于 2019-12-07 02:50:25
问题 I'd like to reuse a script as a dynamic assembly in another non-scripting Roslyn compilation, but I can't for the life of me figure out how to make that work. For example, say a I create a script the normal way and then emit the script as an assembly to a byte stream using something like: var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); var compilation = script.GetCompilation().WithOptions(compilationOptions); using (var ms = new MemoryStream()) {

calling Roslyn from VSIX command

老子叫甜甜 提交于 2019-12-07 02:32:08
问题 What is best way to obtain Roslyn's SyntaxTree from EnvDTE.ProjectItem? I found some method for the other way (Roslyn's Document into ProjectItem). I got VSIX command called from opened document and I'd like to experiment with Roslyn's syntax tree there. This code works, but looks awkward to me: var pi = GetProjectItem(); var piName = pi.get_FileNames(1); var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel)); var workspace =

Why does the IL set this value twice?

爷,独闯天下 提交于 2019-12-07 01:24:41
问题 I was trying around a bit with Try Roslyn when I entered this piece of code: using System; using System.Linq; using System.Collections.Generic; using Microsoft.CSharp; public class C { public C() { x = 4; } public int x { get; } = 5; } And it gave me back this code: using System; using System.Diagnostics; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Security.Permissions; [assembly: AssemblyVersion("0.0.0.0")] [assembly: Debuggable