roslyn

Getting the fully qualified name of a type from a TypeInfo object

不想你离开。 提交于 2019-12-04 16:45:23
问题 Is it somehow possible to get the fully qualified name of the type contained in a TypeInfo object? In the debugger many of these values nicely show up as System.Int32 but when it's printed out, not one of them contains this fully qualified name. I need this to provide as an argument to Type.GetType() . var typeInfo = semanticModel.GetTypeInfo(argument); var w = typeInfo.ToString(); // Microsoft.CodeAnalysis.TypeInfo var y = typeInfo.Type.ToString(); // int var z = typeInfo.Type

Null propagation operator and dynamic variable

本小妞迷上赌 提交于 2019-12-04 16:24:03
问题 I have been looking at the null-propagation operator in C#6 and tried to make it work with the variables of dynamic type but without success. Consider the code below, it compiles but CLR throws AccessViolationException at runtime when the null-propagation is applied to dynamic object. class SomeType { public object SomeProperty { get; set; } static void Main() { var obj = new SomeType() { SomeProperty = "ABCD" }; var p1 = ((dynamic)obj).SomeProperty; //OK, p1 is set to "ABCD" var p2 = (

C# Roslyn change type of comment

爷,独闯天下 提交于 2019-12-04 15:48:28
I'm trying to make an extension for Visual Studio that changes some syntax in the code. Actually, I've done the first step which was to change the name of a variable if this name was not okey with the rules we use in the firm. For example: int newVariable; double test; will be changed into: int iNewVariable; double dblTest; Now I have to change this type of comment: (SingleLineComment) //this is a single line Comment Into a MultiLineComment /*Here it's a MultiLine one*/ I use the Roslyn Syntax Visualiser to find the type and kind to make a correct code, but nothing works. Here it's what I've

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

倖福魔咒の 提交于 2019-12-04 14:55:48
问题 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

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

南楼画角 提交于 2019-12-04 14:08:16
问题 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

Find all method calls for a specific method using Roslyn

♀尐吖头ヾ 提交于 2019-12-04 13:41:55
I am working on a code analyser using Roslyn and my current task is to find all internal methods which are unused in the assembly. I start with a MethodDeclarationSyntax and get the symbol from that. I then use the FindCallersAsync method in SymbolFinder , but it returns an empty collection even when I am making a call to the method in question somewhere in the assembly. See the code below. protected override void Analyze(SyntaxNodeAnalysisContext context) { NodeToAnalyze = context.Node; var methodDeclaration = NodeToAnalyze as MethodDeclarationSyntax; if (methodDeclaration == null) return;

Get Symbol for ReferenceLocation

守給你的承諾、 提交于 2019-12-04 12:47:01
I'm using the SymbolFinder to find all references to a certain type in my solution like this: ISymbol typeOfInterest = compilation.GetTypeByMetadataName( "System.Reflection.PropertyInfo"); var references = SymbolFinder.FindReferencesAsync(typeOfInterest, solution).Result; foreach (var reference in references) { // reference.Locations => symbol? } This part is working fine, the SymbolFinder returns correct ReferenceLocations (upon manual inspection). I'm actually interested in the symbols at these locations to get more (semantic) information about the references, so I can filter upon / work

Roslyn: get the symbol for a type defined in a third-party library

喜欢而已 提交于 2019-12-04 11:37:04
using Roslyn/Microsoft.CodeAnalysis, how can I get the ISymbol for a third-party type, ie, a type defined in a referenced assembly which is not part of the solution? In my particular case, I'm looking for JSON.NET's JObject, but actually the same question would be valid for BCL stuff like StringBuilder etc. The only idea I've come up so far is to implement a CSharpSyntaxWalker which looks for all method invocations, property accesses and constructor calls, checks whether they are made on the type I'm interested in and if yes, gets the symbol from the respective SyntaxNode. I'm going to

Roslyn: Convert C# to VB

烂漫一生 提交于 2019-12-04 10:49:01
I have the case wherein I need to convert a C#- to a VB.NET project. (I want to automate this, so I cannot use an online tool or something similar) There is a "Paste as C#/VB" sample visual studio extension which seemed to be able to do this. I tried converting this class: namespace TestApplication { class Class1 { /// <summary> /// Lorem /// </summary> public void Lorem() { } } } But it ended up with this: Namespace TestApplication Class Class1 ''' <summary> Lorem </summary> Public Sub Lorem() End Sub End Class End Namespace It does not only happen when XML-documentation comments are provided

Roslyn, passing values through hostObject

前提是你 提交于 2019-12-04 10:05:11
I am trying to send one class through hostObject but apparently it doesn't want to work: using Roslyn.Compilers; using Roslyn.Compilers.CSharp; using Roslyn.Scripting; using Roslyn.Scripting.CSharp; public class ShippingService { public class ShippingDetails//class that I want to send { public decimal total { get; set; } public int quantity { get; set; } public string destination { get; set; } } public static string ShipingCost(decimal total, int quantity, string destination) { var details = new ShippingDetails { total = total, quantity = quantity, destination = destination }; try {