roslyn

Roslyn API documentation [closed]

可紊 提交于 2019-12-05 00:56:14
I've developing with the Roslyn CTP, and I'm using the walkthroughs on MSDN and other code samples people have posted to learn. However, I can't find any documentation that explains the various classes and methods that are part of the API. I understand that the API is still in development, but is there a source that explains the API from a high level? I'm looking for documentation like you would normally find in MSDN for the .NET classes. Other than the code samples, I'm left guessing what some of the classes and methods are designed for. You can find Roslyn reference documentation at http:/

call instead of callvirt in case of the new c# 6 “?” null check

久未见 提交于 2019-12-05 00:35:12
Given the two methods: static void M1(Person p) { if (p != null) { var p1 = p.Name; } } static void M2(Person p) { var p1 = p?.Name; } Why the M1 IL code use callvirt : IL_0007: brfalse.s IL_0012 IL_0009: nop IL_000a: ldarg.0 IL_000b: callvirt instance string ConsoleApplication4.Person::get_Name() and the M2 IL use call : brtrue.s IL_0007 IL_0004: ldnull IL_0005: br.s IL_000d IL_0007: ldarg.0 IL_0008: call instance string ConsoleApplication4.Person::get_Name() I just can guess that it because in M2 we know that p isn't null and its like new MyClass().MyMethod(); Is it true? If it is, what if p

Code Formatting in Roslyn SDK Preview

假如想象 提交于 2019-12-05 00:35:08
In an earlier version (Roslyn CTP), I was using following code to format my generated code and it was working perfectly fine: SyntaxNode.Format(FormattingOptions.GetDefaultOptions()).GetFormattedRoot() With the new Roslyn version it no longer does, so what is the equivalent for the above code in the new version (SDK Preview)? You can format SyntaxNodes using the Microsoft.CodeAnalysis.Formatting.Formatter like this (if you have a workspace): using Microsoft.CodeAnalysis.Formatting; var formattedResult = Formatter.Format(syntaxNode, workspace); EDIT : As Jeroen wrote in a comment, if you don't

Creating an EF CodeFirst DbContext using Roslyn

拈花ヽ惹草 提交于 2019-12-04 23:47:29
问题 Just a little idea I'm playing with, not sure if it's viable or has much of a use. I'm trying to generate a very basic EF Code First database using the Roslyn CTP. Code: var scriptEngine = new ScriptEngine(new[] { "System", "System.Core", typeof(DbContext).Assembly.Location }); var session = Roslyn.Scripting.Session.Create(); var t = scriptEngine.CompileSubmission<DbContext>(@" using System.Data.Entity; public class Car { public int Id {get; set;} public string Name {get; set; } } public

Getting Class FullName (including namespace) from Roslyn ClassDeclarationSyntax

偶尔善良 提交于 2019-12-04 23:41:18
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? Jordan you can do this using the helper class I wrote: NamespaceDeclarationSyntax

Roslyn throws The language 'C#' is not supported

坚强是说给别人听的谎言 提交于 2019-12-04 22:47:49
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.HostWorkspaceServices.GetLanguageServices(String languageName) at: Microsoft.CodeAnalysis.Host.Mef

Replacing a method node using Roslyn

为君一笑 提交于 2019-12-04 21:07:25
问题 While exploring Roslyn I put together a small app that should include a trace statement as the first statement in every method found in a Visual Studio Solution. My code is buggy and is only updating the first method. The line that is not working as expected is flagged with a “TODO” comment. Please, advise. I also welcome style recommendations that would create a more streamlined/readable solution. Thanks in advance. ... private void TraceBtn_Click(object sender, RoutedEventArgs e) { var

How do I get a IMethodSymbol or the syntax node of the method declaration from a InvocationExpressionSyntax node which calls the method?

和自甴很熟 提交于 2019-12-04 20:37:52
In the implementation of my analyzer I am in the AnalyzeSymbol method: public override void Initialize(AnalysisContext context) { context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.Method); } private static void AnalyzeSymbol(SymbolAnalysisContext context) { // here I look through the syntax of the method body in order to find invocations. var methodSymbol = context.Symbol as IMethodSymbol; var location = methodSymbol.Locations.FirstOrDefault(); var methodNode = location.SourceTree.GetCompilationUnitRoot().FindNode(locati‌​on.SourceSpan); var blockNode = methodNode.ChildNodes()

Replacing multiple nodes in Roslyn syntax tree

拥有回忆 提交于 2019-12-04 17:23:47
问题 I'm trying to replace a couple of nodes in a syntax tree using roslyn. But the immutable nature of it seems to get in my way. public static string Rewrite(string content) { var tree = CSharpSyntaxTree.ParseText(content); var root = tree.GetRoot(); var methods =root .DescendantNodes(node=>true) .OfType<MethodDeclarationSyntax>() .ToList(); foreach(var method in methods) { var returnActions = method .DescendantNodes(node => true) .OfType<BinaryExpressionSyntax>() //Ok this is cheating .Where

VSTS Build fails/Publish fails to find roslyn\\csc.exe in the bin folder

别来无恙 提交于 2019-12-04 17:23:28
We have a website project which has the following nuget packages installed Microsoft.CodeDom.Providers.DotNetCompilerPlatform - 1.0.8 Microsoft.Net.Compilers - 2.4.0 The web.config has the following <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript"