Created
February 7, 2018 23:14
-
-
Save joncloud/af8923f05d41f0afa7049e85ed796a34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Project Sdk="Microsoft.NET.Sdk"> | |
| <PropertyGroup> | |
| <OutputType>Exe</OutputType> | |
| <TargetFramework>netcoreapp2.0</TargetFramework> | |
| </PropertyGroup> | |
| <ItemGroup> | |
| <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.6.1" /> | |
| </ItemGroup> | |
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.CodeAnalysis; | |
| using Microsoft.CodeAnalysis.CSharp; | |
| using Microsoft.CodeAnalysis.CSharp.Syntax; | |
| using System; | |
| using System.Linq; | |
| namespace ConsoleApp7 | |
| { | |
| public class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| string text = @" | |
| public class Calculator | |
| { | |
| public int IntegerDivision(int x, int y) | |
| { | |
| return x / y; | |
| } | |
| public decimal DecimalDivision(decimal x, decimal y) | |
| { | |
| return x / y; | |
| } | |
| public decimal MixedDivision(decimal x, int y) | |
| { | |
| return x / y; | |
| } | |
| public decimal ConstDivision(decimal x) | |
| { | |
| return x / 1.0M; | |
| } | |
| } | |
| "; | |
| var tree = CSharpSyntaxTree.ParseText(text); | |
| var root = tree.GetCompilationUnitRoot(); | |
| var compilation = CSharpCompilation.Create("test") | |
| .AddReferences( | |
| MetadataReference.CreateFromFile( | |
| typeof(object).Assembly.Location)) | |
| .AddSyntaxTrees(tree); | |
| var semanticModel = compilation.GetSemanticModel(tree); | |
| var divideExpressions = root.DescendantNodes() | |
| .Where(node => node.Kind() == SyntaxKind.DivideExpression) | |
| .Cast<BinaryExpressionSyntax>(); | |
| foreach (var node in divideExpressions) | |
| { | |
| void TestSymbol(ISymbol symbol) | |
| { | |
| switch (symbol.Kind) | |
| { | |
| case SymbolKind.Parameter: | |
| var paramSymbol = (IParameterSymbol)symbol; | |
| Console.WriteLine(paramSymbol.Type.Name); | |
| break; | |
| } | |
| } | |
| void TestSyntaxNode(SyntaxNode n) | |
| { | |
| switch (n.Kind()) | |
| { | |
| case SyntaxKind.IdentifierName: | |
| var identifierName = n as IdentifierNameSyntax; | |
| var symbol = semanticModel.GetSymbolInfo(identifierName); | |
| Console.Write("Identifier: "); | |
| Console.Write(identifierName.Identifier.Value); | |
| Console.Write(":\t"); | |
| TestSymbol(symbol.Symbol); | |
| break; | |
| case SyntaxKind.NumericLiteralExpression: | |
| var numericLiteral = node.Right as LiteralExpressionSyntax; | |
| Console.Write("Literal: "); | |
| Console.WriteLine(numericLiteral.Token.Value); | |
| break; | |
| } | |
| } | |
| TestSyntaxNode(node.Left); | |
| TestSyntaxNode(node.Right); | |
| Console.Write("\t"); | |
| Console.WriteLine(node); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment