roslyn

How add existing project to c# solution with roslyn?

心已入冬 提交于 2019-12-08 13:44:51
问题 I want to add an existing project to my c# solution.I used TryApplyChanges and it returns true but it doesn't save changes to real Solution. I used Microsoft.CodeAnalysis.* version 1.1.1. public void AddProject (string solutionName) { MSBuildWorkspace ws=MSBuildWorkspace.Create (); ws.OpenSolutionAsync (solutionName); ws.OpenProjectAsync ("ProjectName"); if (ws.TryApplyChanges (ws.CurrentSolution ) {// break point is here } } 回答1: There's a few problems with your code: MSBuildWorkspace doesn

Roslyn Rename variable const in Majusucle

我的梦境 提交于 2019-12-08 12:41:21
问题 Trying to convert that: const string maj = "variable"; in const string MAJ = "variable"; I'm using a Diagnostic with CodeFix. I've already done the Diagnostic: var localDeclarationConst = node as LocalDeclarationStatementSyntax; if (localDeclarationConst != null && localDeclarationConst.Modifiers.Any(SyntaxKind.ConstKeyword) ) { foreach (VariableDeclaratorSyntax variable in localDeclarationConst.Declaration.Variables) { var symbol = model.GetDeclaredSymbol(variable); if (symbol != null) {

using Roslyn to parse classes functions and properties

左心房为你撑大大i 提交于 2019-12-08 08:37:28
问题 I am new to Roslyn and I am trying to parse a cs file and get out the functions and properties. API Examples //won't work any more var tree = SyntaxTree.ParseText(...); var tree = SyntaxTree.ParseFle(...); var root = tree.Root; API seems to have changed and it won't work any more. Guess it works with 2012 Roslyn CTP only by the look of it. I then try the newer example: //won't work neither var compilation = CSharpCompilation.Create("HelloWorld") .AddReferences( new MetadataFileReference(

Xamarin Android runtime C# compilation

夙愿已清 提交于 2019-12-08 07:07:30
问题 I am trying to compile a simple code in C# using Microsoft.CodeAnalysis.CSharp . The problem is in trying to add the reference to mscorlib.dll . As the CreateFromAssembly method of the MetadataReference class is deprecated I am trying to use the CreateFromFile method (as suggested): var mscorlibAssembly = Assembly.Load(new AssemblyName("mscorlib.dll")).ManifestModule.Assembly; var mscorlibref = MetadataReference.CreateFromFile(mscorlibAssembly.Location); result.Add(mscorlibref); The Location

Using Roslyn, how to check if class comes from a local project, not the BCL or Nuget (etc)?

☆樱花仙子☆ 提交于 2019-12-08 07:02:37
问题 I want to write a Roslyn code analyser; which needs to work out if an ObjectCreationExpression is creating an object from a local class (either in the current project or a project in the current solution); or if the class comes from somewhere else, like the Base Class Library or a Nuget package etc. How do I tell where a class comes from in Roslyn? 回答1: You can only get that with the help of the semantic model. You can get the symbol for the constructor, and the check where the type comes

Roslyn Analyzer which compares against separate class file

↘锁芯ラ 提交于 2019-12-08 05:22:28
问题 I'm writing a Roslyn analyzer which compares checks if const are already declared in a Localization Resx. public const string DiagnosticId = "LocalizationTool"; // You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat. // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/Localizing%20Analyzers.md for more on localization private static readonly LocalizableString Title

How to add custom syntax annotation to SyntaxNode?

本小妞迷上赌 提交于 2019-12-08 04:00:26
问题 In previous version of Roslyn I could add custom data to SyntaxNode with using class derived from SyntaxAnnotation . But now SyntaxAnnotation is sealed and I can use SyntaxAnnotation only with to strings: kind and data. It is not enough for me. I want to build custom CFG from AST and make bidirectional links between CFG and AST nodes. How I can do that? 回答1: SyntaxAnnotation is sealed because your tree may be serialized, and we need to be able to serialize and deserialize the annotations. If

How to GetSemanticModel for any syntax tree in referenced projects of project compilation

五迷三道 提交于 2019-12-08 03:17:28
问题 When I compile a project (using MSBuildWorkspace.Create().OpenProjectAsync().GetCompilationAsync(); I get a Compilation back which includes x SyntaxTrees for the x files within that project. But that project also references other projects. I can see the those projects within the References property of the Compilation , but it seems that calling Compliation.GetSemanticModel does not look through these. For an arbitrary SyntaxTree that is somewhere within my project (including referenced

How do I get a Roslyn workspace from a VS package?

丶灬走出姿态 提交于 2019-12-07 22:01:01
问题 From a Visual Studio 2015 CTP5 package, how do I get the current Roslyn workspace? I looked at How to get reference to 'Roslyn' Workspace object from IVsSolution? and Roslyn: How to get a reference to Workspace from currently loaded solution? but I still can't make it work: Workspace.CurrentWorkspace does not exist anymore I have tried importing the VisualStudioWorkspace but it is still null: public sealed class VSPackage1Package : Package { .... [Import] public VisualStudioWorkspace

Is there a way to setup a working directory for “r and ”load commands

浪尽此生 提交于 2019-12-07 21:12:37
问题 Quesiton is relative to References in Roslyn .rsp files Did install the Roslyn, playing with: C:\Users\name\Documents\Microsoft Codename Roslyn CTP - October 2011\CSharp\WpfSeedRepl Walkthrough: http://msdn.microsoft.com/ru-ru/hh543924. I'm unable to load current DLL and csx: > #r "wpfseed.exe" (1,1): error CS0006: Metadata file 'wpfseed.exe' could not be found > #load "setup.csx" Specified file not found. Searched in directory: C:\Users\name > On other hand: > Console.WriteLine(Environment