Created
March 7, 2017 18:45
-
-
Save azyobuzin/77c0b5ee667e683a4afd3aff9189d693 to your computer and use it in GitHub Desktop.
IOperation で遊ぶぞ
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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.CodeAnalysis; | |
using Microsoft.CodeAnalysis.CSharp; | |
using Microsoft.CodeAnalysis.CSharp.Syntax; | |
using Microsoft.CodeAnalysis.Semantics; | |
public static class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var source = @"class Program { static void Main(string[] args) { System.Console.WriteLine(""Hello""); } }"; | |
var tree = CSharpSyntaxTree.ParseText( | |
source, | |
new CSharpParseOptions().WithFeatures(new[] | |
{ | |
new KeyValuePair<string, string>("IOperation", "") | |
}) | |
); | |
var compilation = CSharpCompilation.Create( | |
"Test", | |
syntaxTrees: new[] { tree }, | |
references: new[] { MetadataReference.CreateFromFile(@"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\mscorlib.dll") }, | |
options: new CSharpCompilationOptions(OutputKind.ConsoleApplication) | |
); | |
var semanticModel = compilation.GetSemanticModel(tree); | |
var operation = semanticModel.GetOperation( | |
tree.GetRoot().DescendantNodes().FirstOrDefault(x => x is InvocationExpressionSyntax) | |
); | |
Console.WriteLine(operation.GetType().FullName); | |
Console.WriteLine(operation.Kind); | |
var callOperation = (IInvocationExpression)operation; | |
Console.WriteLine(callOperation.TargetMethod); | |
foreach (IArgument arg in callOperation.ArgumentsInParameterOrder) | |
{ | |
Console.WriteLine(arg.Value.Syntax); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment