roslyn

Roslyn: How to load an existing project outside of Visual Studio

北城余情 提交于 2019-12-23 15:25:56
问题 As written in this question: Can I access the MsBuildWorkspace from within a MsBuild Task class? You cannot use MSBuildWorkspace from an MSBuild task for the same project. However, the only 2 other options are VisualStudioWorkspace and AdhocWorkspace . Does this mean that there's no option for loading a project from an MSBuild task? 回答1: You can create a Project from a set command line arguments using CommandLineProject.CreateProjectInfo , so if you have the csc command line, you can use that

Why does initial hash value in GetHashCode() implementation generated for anonymous class depend on property names?

自闭症网瘾萝莉.ら 提交于 2019-12-23 10:15:24
问题 When generating GetHashCode() implementation for anonymous class, Roslyn computes the initial hash value based on the property names. For example, the class generated for var x = new { Int = 42, Text = "42" }; is going to have the following GetHashCode() method: public override in GetHashCode() { int hash = 339055328; hash = hash * -1521134295 + EqualityComparer<int>.Default.GetHashCode( Int ); hash = hash * -1521134295 + EqualityComparer<string>.Default.GetHashCode( Text ); return hash; }

How do I enable C# scripting in VS 2015?

做~自己de王妃 提交于 2019-12-23 08:50:03
问题 How do I configure VS 2015 to enable Roslyn's C# scripting capabilities? I've tried installing various Nuget packages, including both the 1.0 and 1.1.0-beta1 versions of Microsoft.CodeAnalysis.CSharp, Microsoft.CodeAnalysis.Scripting.CSharp, etc., but I can't get any of the examples I've found online to work. I am getting "type not found" errors, i.e., var scriptEngine = new ScriptEngine(); ... fails because the type "ScriptEngine" is not found. Can someone provide as recipe that includes

Using Roslyn for C#, how do I get a list of all properties that compose a return type?

北城余情 提交于 2019-12-23 08:48:18
问题 Let's say that I have queried a single method from a collection of methods: var myMethod = someListofMethods.FirstOrDefault(m => m.Identifier.ValueText == myMethodName); Now I want to take the method's return type. . . var returnType = myMethod.ReturnType; . . .and determine (if it is not a primitive) what properties are contained within that type. So, for example let's say the return type is FooObject which is defined: public class FooObject{ public string Fizz {get; set; } public string

Roslyn : How to get the Namespace of a DeclarationSyntax with Roslyn C#

这一生的挚爱 提交于 2019-12-23 08:04:12
问题 I have a c# solution that contains some class files. With Roslyn I am able to parse a solution to obtain a list of projects within the solution. From there, I can get the documents in each project. Then, I can get a list of every ClassDeclarationSyntax. This is the starting point. foreach (var v in _solution.Projects) { //Console.WriteLine(v.Name.ToString()); foreach (var document in v.Documents) { SemanticModel model = document.GetSemanticModelAsync().Result; var classes = document

Roslyn not finding all references

左心房为你撑大大i 提交于 2019-12-23 05:32:15
问题 I'm using Roslyn to rewrite some methods used throughout a solution. I was able to find and fix many instances. However, it's not finding all references. Many more show up in Visual Studio with CodeLens or using "Find All References." The solution compiles with no errors. The references it's not finding seem to be only overloaded methods called from web applications in the solution. But it finds references to non-overloaded methods in these web applications. I'm finding the symbols for the

Compilation succeeded even though there were some errors from Roslyn Diagnostic Analyzer

走远了吗. 提交于 2019-12-23 00:43:26
问题 The DiagnosticAnalyzer is a custom Roslyn based extension whose DiagnosticDescriptor with DiagnosticSeverity.Error is as shown below internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Description, MessageFormat, Category, DiagnosticSeverity.Error); When the extension is used it does show the red squiggles over the codes that is against the Diagnostic Analyzers custom rule emphasising that it is an error , it is even shown in the Error List window of Visual

How to remove a list of SyntaxNode from the Document using Roslyn code fix provider APIs?

ⅰ亾dé卋堺 提交于 2019-12-22 12:34:12
问题 I'm having a custom generated variable declaration using SyntaxFactory.VariableDeclaration and a list of SyntaxNode 's collected according to some conditions. I did the following: Modify the node var newRoot = root.ReplaceNode(expression, newVariableDeclaration) This successfully modified the node with the newVariableDeclaration . In a loop remove the nodes corresponding to the ones present in the list foreach (var listObject in listObjects) { newRoot = newRoot.RemoveNode(listObject,

SymbolFinder.FindReferencesAsync doesn't find anything

你说的曾经没有我的故事 提交于 2019-12-22 12:30:09
问题 I want to find all PropertyChangedEventHandler events in my solution, and then find all listeners added to those events. But I can't seem to get a list of the events. This is all the code in the solution being analyzed: using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RoslynTarget { public class Example : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged =

Opening a solution with msbuildworkspace gives diagnostics errors without details

*爱你&永不变心* 提交于 2019-12-22 11:18:44
问题 I am trying to analyse a solution with Roslyn, with MSBuildWorkspace. The solution is a new solution, with 2 class library projects in them, one referencing the other. They are created in Visual Studio 2017, .Net 4.6.2. When I open the solution, I receive two generic errors in workspace.Diagnostics, both are : Msbuild failed when processing the file ' PathToProject ' There is nothing more in the diagnostics or output window, to indicate WHY it failed to process the project file. The code for