Skip to content

Commit 413a224

Browse files
committed
Add typeinfo of roslyn example
1 parent 00b0a82 commit 413a224

File tree

7 files changed

+173
-0
lines changed

7 files changed

+173
-0
lines changed

Roslyn/Roslyn.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 14.0.25420.1
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NumericLiteral", "NumericLiteral\NumericLiteral.xproj", "{D46130B0-8F62-40C0-BC30-22C2A0788C9A}"
77
EndProject
8+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TypeInfo", "TypeInfo\TypeInfo.xproj", "{D168F4EF-54C1-444D-9ECA-CAAEEBE62B74}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{D46130B0-8F62-40C0-BC30-22C2A0788C9A}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{D46130B0-8F62-40C0-BC30-22C2A0788C9A}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{D46130B0-8F62-40C0-BC30-22C2A0788C9A}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{D168F4EF-54C1-444D-9ECA-CAAEEBE62B74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{D168F4EF-54C1-444D-9ECA-CAAEEBE62B74}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{D168F4EF-54C1-444D-9ECA-CAAEEBE62B74}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{D168F4EF-54C1-444D-9ECA-CAAEEBE62B74}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

Roslyn/TypeInfo/Program.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyConfiguration("")]
9+
[assembly: AssemblyCompany("")]
10+
[assembly: AssemblyProduct("TypeInfo")]
11+
[assembly: AssemblyTrademark("")]
12+
13+
// Setting ComVisible to false makes the types in this assembly not visible
14+
// to COM components. If you need to access a type in this assembly from
15+
// COM, set the ComVisible attribute to true on that type.
16+
[assembly: ComVisible(false)]
17+
18+
// The following GUID is for the ID of the typelib if this project is exposed to COM
19+
[assembly: Guid("d168f4ef-54c1-444d-9eca-caaeebe62b74")]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"TypeInfo": {
4+
"commandName": "Project",
5+
"commandLineArgs": "> result.txt"
6+
}
7+
}
8+
}

Roslyn/TypeInfo/TypeInfo.xproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>d168f4ef-54c1-444d-9eca-caaeebe62b74</ProjectGuid>
11+
<RootNamespace>TypeInfo</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>

Roslyn/TypeInfo/project.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "1.0.0-*",
3+
"buildOptions": {
4+
"emitEntryPoint": true
5+
},
6+
7+
"dependencies": {
8+
"Microsoft.NETCore.App": {
9+
"type": "platform",
10+
"version": "1.0.1"
11+
},
12+
"Microsoft.CodeAnalysis.Analyzers": "1.1.0",
13+
"Microsoft.CodeAnalysis.Common": "1.3.2",
14+
"Microsoft.CodeAnalysis.CSharp": "1.3.2",
15+
"Microsoft.CodeAnalysis.CSharp.Workspaces": "1.3.2",
16+
"Microsoft.CodeAnalysis.Workspaces.Common": "1.3.2",
17+
"System.Collections.Immutable": "1.2.0",
18+
"System.Reflection.Metadata": "1.3.0"
19+
},
20+
21+
"frameworks": {
22+
"netcoreapp1.0": {
23+
"imports": "portable-net45+win8+wp8+wpa81" // for Microsoft.Composition
24+
}
25+
}
26+
}

Roslyn/TypeInfo/result.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1.0 + foo : double, (minimum)double, (converted)object
2+
typeof(Class1) : System.Type, (minimum)Type, (converted)object
3+
4+
first Method2(array, array, array)
5+
No.0 : object[], (minimum)object[], (converted)object[]
6+
No.1 : object[], (minimum)object[], (converted)System.Collections.Generic.IEnumerable<object>
7+
No.2 : object[], (minimum)object[], (converted)System.Collections.IEnumerable
8+
9+
second Method2(new Class1[1], null, array as IEnumerable<object>)
10+
No.0 : Class1[], (minimum)Class1[], (converted)object[]
11+
No.1 : , (minimum), (converted)System.Collections.Generic.IEnumerable<object>
12+
No.2 : System.Collections.Generic.IEnumerable<object>, (minimum)IEnumerable<object>, (converted)System.Collections.IEnumerable

0 commit comments

Comments
 (0)