roslyn

Trying to understand ?. (null-conditional) operator in C#

北城余情 提交于 2019-12-03 09:17:11
I have this very simple example: class Program { class A { public bool B; } static void Main() { System.Collections.ArrayList list = null; if (list?.Count > 0) { System.Console.WriteLine("Contains elements"); } A a = null; if (a?.B) { System.Console.WriteLine("Is initialized"); } } } The line if (list?.Count > 0) compiles perfectly which means that if list is null , the expression Count > 0 becomes false by default. However, the line if (a?.B) throws a compiler error saying I can't implicitly convert bool? to bool . Why is one different from the other? Heinzi list?.Count > 0 : Here you compare

How to debug code compiled with Roslyn in a Visual Studio extension inside current Visual Studio host?

≯℡__Kan透↙ 提交于 2019-12-03 08:52:47
I have a Visual Studio extensions that use Roslyn to get a project in current opened solution, compile it and run methods from it. The project can be modified by the programmer. I have successfully compiled a project in a Visual Studio extension from the current VisualStudioWorkspace. private static Assembly CompileAndLoad(Compilation compilation) { using (MemoryStream dllStream = new MemoryStream()) using (MemoryStream pdbStream = new MemoryStream()) { EmitResult result = compilation.Emit(dllStream, pdbStream); if (!result.Success) { IEnumerable<Diagnostic> failures = result.Diagnostics.Where

Get Type of 'var' with Roslyn?

旧巷老猫 提交于 2019-12-03 08:04:30
问题 I've got a .cs file named 'test.cs' which essentially looks like: namespace test { public class TestClass { public void Hello() { var x = 1; } } } I'm trying to parse this with Roslyn and get the type of x, which should be 'int', but I can only find out that it's type 'var', I can't seem to get the actual underlying type. Here's basically what my code is now var location = "test.cs"; var sourceTree = CSharpSyntaxTree.ParseFile(location); var root = (CompilationUnitSyntax)sourceTree.GetRoot();

Finding everywhere an enum is converted to string

最后都变了- 提交于 2019-12-03 05:47:19
I'm currently trying to find everywhere in a solution where a specific enum is converted to a string, whether or not ToString() is explicitly called. (These are being replaced with a conversion using enum descriptions to improve obfuscation.) Example: I'd like to find code such as string str = "Value: " + SomeEnum.someValue; I've tried replacing the enum itself with a wrapper class containing implicit conversions to the enum type and overriding ToString() in the wrapper class, but when I try searching for uses of the ToString() override it gives me a list of places in the solution where

C# REPL outside Visual Studio

℡╲_俬逩灬. 提交于 2019-12-03 05:36:23
问题 F# has a REPL (read–eval–print loop) F# Interactive, C:\Program Files (x86)\Microsoft F#\v4.0\Fsi.exe . I understand C# now has its own interactive REPL, as released with Roslyn. How do I open outside Visual Studio? Where is csi.exe ? 回答1: C# Interactive window and csi.exe REPL were added to Visual Studio 2015 Update 1 (emphasis mine): Introducing Interactive The Interactive Window is back! The C# Interactive Window returns in Visual Studio 2015 Update 1 along with a couple other interactive

Why are there so many implementations of Object Pooling in Roslyn?

最后都变了- 提交于 2019-12-03 04:10:02
问题 The ObjectPool is a type used in the Roslyn C# compiler to reuse frequently used objects which would normally get new'ed up and garbage collected very often. This reduces the amount and size of garbage collection operations which have to happen. The Roslyn compiler seems to have a few separate pools of objects and each pool has a different size. I want to know why there are so many implementations, what the preferred implementation is and why they picked a pool size of 20, 100 or 128. 1 -

How can I make my code diagnostic syntax node action work on closed files?

牧云@^-^@ 提交于 2019-12-03 04:00:53
问题 I'm building a set of code diagnostics using Roslyn (in VS2015 Preview). Ideally, I'd like any errors they produce to act as persistent errors, just as if I were violating a normal language rule. There are a bunch of options, but I'm having a hard time getting any of them to work consistently. I've managed to implement a rudimentary syntax node action, i.e. one registered with context.RegisterSyntaxNodeAction(AnalyzeSyntaxNode, SyntaxKind.InvocationExpression); in the Initialize method of my

Get dependencies between classes in Roslyn

笑着哭i 提交于 2019-12-03 03:34:51
I am successfully getting dependencies between projects with Roslyn, and now I would like to get dependencies between classes, similar to the Code Map feature in Visual Studio Enterprise. Here is my code, the "?????" part is where I imagine I could get something. I am very new to the Roslyn API, though, and I don't know how to proceed from there on. Solution solution = MSBuildWorkspace.Create() .OpenSolutionAsync(Path.Combine(repoRootFolder, "MySolution.sln")) .Result; ProjectDependencyGraph projdeps = solution.GetProjectDependencyGraph(); Digraph graph = new Digraph(); foreach (ProjectId

Roslyn failed to compile code

佐手、 提交于 2019-12-03 02:48:41
问题 After I have migrated my project from VS2013 to VS2015 the project no longer builds. A compilation error occurs in the following LINQ statement: static void Main(string[] args) { decimal a, b; IEnumerable<dynamic> array = new string[] { "10", "20", "30" }; var result = (from v in array where decimal.TryParse(v, out a) && decimal.TryParse("15", out b) && a <= b // Error here orderby decimal.Parse(v) select v).ToArray(); } The compiler returns an error: Error CS0165 Use of unassigned local

visual studio 2015 update 2 RTM and RC debugging context not available

走远了吗. 提交于 2019-12-03 02:38:32
问题 Update 1: After Updating visual studio to version 2015 update 2 RTM the problem still exist. Update 2 patch available 4/12/2016 : Thanks to Patrick Nelson. a patch has been released You can download the patch here. Update 3 8/16/2016: Latest vs 2015 update 3 KB3165756 has the same problem do not update Version Used: Latest stable version on visual studio update 2 rc Disabled all my extensions and also restarted my all settings Steps to Reproduce: Install visual studio update 2 rc Create new