roslyn

Roslyn throws The language 'C#' is not supported

泄露秘密 提交于 2019-12-10 01:23:45
问题 I have created a class library project and did some processing and also used Roslyn to generate code. I use the library in a WPF GUI application as a reference. These are the NuGet packages: Build shows no error, however when I use the following code: private static void GetGenerator() { workspace = new AdhocWorkspace(); generator = SyntaxGenerator.GetGenerator(workspace, LanguageNames.CSharp); } I get an exception: "The language 'C#' is not supported." at: Microsoft.CodeAnalysis.Host

Getting Class FullName (including namespace) from Roslyn ClassDeclarationSyntax

自古美人都是妖i 提交于 2019-12-10 01:02:27
问题 I've a ClassDeclarationSyntax from a syntax tree in roslyn. I read it like this: var tree = SyntaxTree.ParseText(sourceCode); var root = (CompilationUnitSyntax)tree.GetRoot(); var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>(); The identifier only contains the name of the class but no information about the namespace, so the fullType Name is missing. Like "MyClass" but noch "Namespace1.MyClass" what is the recommended way to get the namespace / FulltypeName of the Syntax?

How to get il of one method body with roslyn?

橙三吉。 提交于 2019-12-09 13:05:26
问题 I want to get il of one method in my c# source code file.I have opened solution with roslyn and find the method symbol like below Roslyn.Compilers.Common.ISymbol s=GetMethodSymbolAtPosition (30); I have an ISymbol how get il now? 回答1: Unfortunately, the IL generation is entirely hidden inside the Emit call in Roslyn. But I'll give a simple to get you started. Let's suppose you start of with an existing compilation: var initial = CSharpCompilation.Create("Existing") .AddReferences

Roslyn Analyzer which compares against separate class file

巧了我就是萌 提交于 2019-12-09 06:51:27
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 = new LocalizableResourceString(nameof(Resources.AnalyzerTitle), Resources.ResourceManager, typeof

How to compare a Microsoft.CodeAnalysis.ITypeSymbol to a System.Type

我的梦境 提交于 2019-12-09 06:45:02
问题 I have successfully received an ITypeSymbol from a SyntaxNode by using: SemanticModel.GetTypeInfo(sytaxNode).ConvertedType Now I would like to know if this ITypeSymbol corresponds to a System.Type instance that is present in my executing code, like typeof(IEnumerable<int>) or someObject.GetType() . I tried typeInfo.ConvertedType.ToString() == type.ToString() But these do not use the same formatting rules, for instance for generics like IEnumerable<int> TypeInfo.ToString() == "System

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

痞子三分冷 提交于 2019-12-09 03:42:27
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? m0sa 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 from via Locations or DeclaringSyntaxReferences , e.g.: // ObjectCreationExpression node == ...; //

Why can't I throw exceptions from an expression-bodied member?

血红的双手。 提交于 2019-12-09 02:07:18
问题 Using expression-bodied members allows you to define the body of a method or property as a single expression without a return keyword (should it return something). For example it turns these int Method1() { return 5; } void Method2() { Console.WriteLine(); } into these int Method1() => 5; void Method2() => Console.WriteLine(); A difference comes into play when you throw an exception from the body: void Method3() { throw new Exception(); } However, the following will not compile: void Method3(

Roslyn load project documents failing

 ̄綄美尐妖づ 提交于 2019-12-08 17:28:43
问题 In a Visual Studio Extension (VSIX) solution, I'm using Roslyn to load a specific project from my current solution: Project myProject = this.CurrentComponentModel.GetService<VisualStudioWorkspace>() .CurrentSolution.Projects .FirstOrDefault(p => p.Name == "MyProject") The projct myProject is definitely loaded, but on inspection I see that: myProject.HasDocuments == false myProject.Documents is Empty And yet, in Visual Studio I can see loads of documents. If I close the solution and open the

Why would VS 2017 suggest replacing a property with a method?

北城余情 提交于 2019-12-08 17:24:36
问题 I noticed that every property in Visual Studio 2017 has a quick action that suggests replacing it with a method. Is that mean that properties is not the recommended way to set and get fields value, do Microsoft intend to deprecate it in the future?! Or are there any gains that could be achieved using methods over properties for that purpose? 回答1: This is not a suggestion from Visual Studio, it is a Quick Action: Quick Actions let you easily refactor, generate, or otherwise modify code with a

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

时光怂恿深爱的人放手 提交于 2019-12-08 14:15:31
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 projects) do I have to manually search through each referenced Compilation of the root project or is there