Browse Source

Add a Xamarin-workbooks lookalike for use in .NET Interactive Notebooks (see https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.dotnet-interactive-vscode)

pull/2326/head
Christoph Wille 4 years ago
parent
commit
02c0e7805d
  1. 331
      decompiler-nuget-demos.ipynb

331
decompiler-nuget-demos.ipynb

@ -0,0 +1,331 @@ @@ -0,0 +1,331 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load the NuGet package and print out the version to make sure we are using the latest and greatest"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"source": [
"#r \"nuget: ICSharpCode.Decompiler, 7.0.0.6372-preview3\"\n",
"\n",
"using System.Reflection.Metadata;\n",
"using ICSharpCode.Decompiler;\n",
"using ICSharpCode.Decompiler.CSharp;\n",
"using ICSharpCode.Decompiler.Metadata;\n",
"using ICSharpCode.Decompiler.TypeSystem;\n",
"\n",
"Console.WriteLine(typeof(FullTypeName).Assembly.GetName());"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "ICSharpCode.Decompiler, Version=7.0.0.6372, Culture=neutral, PublicKeyToken=d4bfe873e7598c49\r\n"
},
"execution_count": 1,
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You must have compiled **frontends.sln** first (run “dotnet build” in ICSharpCode.Decompiler.PowerShell folder). Make sure to modify **basePath** to your repository path."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"source": [
"const string basePath = @\"D:\\GitWorkspace\\ILSpy\\\";\n",
"string testAssemblyPath = basePath + @\"ICSharpCode.Decompiler.PowerShell\\bin\\Release\\netstandard2.0\\ICSharpCode.Decompiler.dll\";\n",
"\n",
"var decompiler = new CSharpDecompiler(testAssemblyPath, new DecompilerSettings());"
],
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get the count of types in this module"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"source": [
"var types = decompiler.TypeSystem.MainModule.TypeDefinitions;\n",
"Console.WriteLine(types.Count());"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "1459\r\n"
},
"execution_count": 1,
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Decompile a known type (as a whole)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"source": [
"// ICSharpCode.Decompiler.Util.Empty<T> -> translates to `n, where n is the # of generic parameters\n",
"var nameOfGenericType = new FullTypeName(\"ICSharpCode.Decompiler.Util.Empty`1\");\n",
"Console.WriteLine(decompiler.DecompileTypeAsString(nameOfGenericType));"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "using System;\r\n\r\nnamespace ICSharpCode.Decompiler.Util\r\n{\r\n\tpublic static class Empty<T>\r\n\t{\r\n\t\tpublic static readonly T[] Array = System.Array.Empty<T>();\r\n\t}\r\n}\r\n\r\n"
},
"execution_count": 1,
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you want to decompile one single member (sample: first method)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"source": [
"var nameOfUniResolver = new FullTypeName(\"ICSharpCode.Decompiler.Metadata.UniversalAssemblyResolver\");\n",
"ITypeDefinition typeInfo = decompiler.TypeSystem.FindType(nameOfUniResolver).GetDefinition();\n",
"var tokenOfFirstMethod = typeInfo.Methods.First().MetadataToken;\n",
"Console.WriteLine(decompiler.DecompileAsString(tokenOfFirstMethod));"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "using System;\r\n\r\nstatic UniversalAssemblyResolver()\r\n{\r\n\tgac_paths = GetGacPaths();\r\n\tZeroVersion = new Version(0, 0, 0, 0);\r\n\tif (Type.GetType(\"Mono.Runtime\") != null)\r\n\t{\r\n\t\tdecompilerRuntime = DecompilerRuntime.Mono;\r\n\t}\r\n\telse if (typeof(object).Assembly.GetName().Name == \"System.Private.CoreLib\")\r\n\t{\r\n\t\tdecompilerRuntime = DecompilerRuntime.NETCoreApp;\r\n\t}\r\n\telse if (Environment.OSVersion.Platform == PlatformID.Unix)\r\n\t{\r\n\t\tdecompilerRuntime = DecompilerRuntime.Mono;\r\n\t}\r\n}\r\n\r\n"
},
"execution_count": 1,
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you need access to low-level metadata tables"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"source": [
"ITypeDefinition type = decompiler.TypeSystem.FindType(nameOfUniResolver).GetDefinition();\n",
"var module = type.ParentModule.PEFile;"
],
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get the child namespaces"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"source": [
"var icsdns = decompiler.TypeSystem.RootNamespace;\n",
"foreach (var cn in icsdns.ChildNamespaces) Console.WriteLine(cn.FullName);"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "Microsoft\r\n"
},
"execution_count": 1,
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": "System\r\n"
},
"execution_count": 1,
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": "LightJson\r\n"
},
"execution_count": 1,
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": "Humanizer\r\n"
},
"execution_count": 1,
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": "ICSharpCode\r\n"
},
"execution_count": 1,
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": "FxResources\r\n"
},
"execution_count": 1,
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": "Internal\r\n"
},
"execution_count": 1,
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": "Windows\r\n"
},
"execution_count": 1,
"metadata": {}
},
{
"output_type": "execute_result",
"data": {
"text/plain": "MS\r\n"
},
"execution_count": 1,
"metadata": {}
}
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get types in a single namespace"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"source": [
"var typesInNamespace = icsdns.ChildNamespaces.First(cn => cn.FullName == \"LightJson\").Types;\n",
"Console.WriteLine(typesInNamespace.First().FullTypeName);"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": "LightJson.JsonArray\r\n"
},
"execution_count": 1,
"metadata": {}
}
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
}
},
"source": [
""
],
"outputs": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "8.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading…
Cancel
Save