1+ using System ;
2+ using System . Linq ;
3+ using Microsoft . CodeAnalysis ;
4+ using Microsoft . CodeAnalysis . CSharp ;
5+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
6+ using System . Reflection ;
7+
8+ namespace TypeInfo
9+ {
10+ public class Program
11+ {
12+ public static void Main ( string [ ] args )
13+ {
14+ var code = @"
15+ using System.Collections;
16+ using System.Collections.Generic;
17+ class Class1
18+ {
19+ void Method1()
20+ {
21+ int foo = 1;
22+ var array = new object[] {
23+ 1.0 + foo, // double
24+ typeof(Class1) // System.Type
25+ };
26+
27+ // first
28+ Method2(array, array, array);
29+
30+ // second
31+ Method2(new Class1[1], null, array as IEnumerable<object>);
32+ }
33+
34+ // this is just called, do nothing.
35+ void Method2(object[] _, IEnumerable<object> __, IEnumerable ___) { }
36+ }" ;
37+
38+ var tree = CSharpSyntaxTree . ParseText ( code ) ;
39+ var mscorlib = MetadataReference . CreateFromFile ( typeof ( object ) . GetTypeInfo ( ) . Assembly . Location ) ;
40+ var compilation = CSharpCompilation . Create ( "NumericLiteral" , new [ ] { tree } , new [ ] { mscorlib } ) ;
41+ var model = compilation . GetSemanticModel ( tree ) ;
42+
43+
44+ var root = tree . GetRoot ( ) ;
45+ var values = root . DescendantNodes ( ) . OfType < ArrayCreationExpressionSyntax > ( ) . First ( )
46+ . ChildNodes ( ) . OfType < InitializerExpressionSyntax > ( ) . First ( )
47+ . ChildNodes ( ) ;
48+ foreach ( var item in values )
49+ {
50+ var info = model . GetTypeInfo ( item ) ;
51+ Console . WriteLine ( $ "{ item . WithoutTrivia ( ) . ToFullString ( ) } : { info . Type } , "
52+ + $ "(minimum){ info . Type ? . ToDisplayString ( SymbolDisplayFormat . MinimallyQualifiedFormat ) } , "
53+ + $ "(converted){ info . ConvertedType } ") ;
54+ }
55+ Console . WriteLine ( ) ;
56+
57+ var calling = root . DescendantNodes ( ) . OfType < InvocationExpressionSyntax > ( ) . First ( ) ;
58+ var symbols = calling . ArgumentList . Arguments . Select ( a => a . Expression ) ;
59+ Console . WriteLine ( "first " + calling . WithoutTrivia ( ) . ToString ( ) ) ;
60+ foreach ( var item in symbols . Select ( ( v , i ) => new { Index = i , Value = v } ) )
61+ {
62+ var info = model . GetTypeInfo ( item . Value ) ;
63+ Console . WriteLine ( $ "No.{ item . Index } : { info . Type } , "
64+ + $ "(minimum){ info . Type ? . ToDisplayString ( SymbolDisplayFormat . MinimallyQualifiedFormat ) } , "
65+ + $ "(converted){ info . ConvertedType } ") ;
66+ }
67+ Console . WriteLine ( ) ;
68+
69+ calling = root . DescendantNodes ( ) . OfType < InvocationExpressionSyntax > ( ) . Skip ( 1 ) . First ( ) ;
70+ symbols = calling . ArgumentList . Arguments . Select ( a => a . Expression ) ;
71+ Console . WriteLine ( "second " + calling . WithoutTrivia ( ) . ToString ( ) ) ;
72+ foreach ( var item in symbols . Select ( ( v , i ) => new { Index = i , Value = v } ) )
73+ {
74+ var info = model . GetTypeInfo ( item . Value ) ;
75+ Console . WriteLine ( $ "No.{ item . Index } : { info . Type } , "
76+ + $ "(minimum){ info . Type ? . ToDisplayString ( SymbolDisplayFormat . MinimallyQualifiedFormat ) } , "
77+ + $ "(converted){ info . ConvertedType } ") ;
78+ }
79+ }
80+ }
81+ }
0 commit comments