You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
8.3 KiB

  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "You must install **.NET Interactive Notebooks** extension in VS Code to run this notebook!\n",
  8. "\n",
  9. "Load the NuGet package and print out the version to make sure we are using the latest and greatest"
  10. ]
  11. },
  12. {
  13. "cell_type": "code",
  14. "execution_count": null,
  15. "metadata": {
  16. "dotnet_interactive": {
  17. "language": "csharp"
  18. }
  19. },
  20. "outputs": [
  21. {
  22. "data": {
  23. "text/html": [
  24. "<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>ICSharpCode.Decompiler, 7.2.0.6702-preview2</span></li></ul></div></div>"
  25. ]
  26. },
  27. "metadata": {},
  28. "output_type": "display_data"
  29. },
  30. {
  31. "name": "stdout",
  32. "output_type": "stream",
  33. "text": [
  34. "ICSharpCode.Decompiler, Version=7.2.0.6702, Culture=neutral, PublicKeyToken=d4bfe873e7598c49\r\n"
  35. ]
  36. }
  37. ],
  38. "source": [
  39. "#r \"nuget: ICSharpCode.Decompiler, 7.2.0.6702-preview2\"\n",
  40. "\n",
  41. "using System.Reflection.Metadata;\n",
  42. "using ICSharpCode.Decompiler;\n",
  43. "using ICSharpCode.Decompiler.CSharp;\n",
  44. "using ICSharpCode.Decompiler.Metadata;\n",
  45. "using ICSharpCode.Decompiler.TypeSystem;\n",
  46. "\n",
  47. "Console.WriteLine(typeof(FullTypeName).Assembly.GetName());"
  48. ]
  49. },
  50. {
  51. "cell_type": "markdown",
  52. "metadata": {},
  53. "source": [
  54. "You must have compiled **ILSpy.XPlat.slnf** first (run “dotnet build” in ICSharpCode.Decompiler.PowerShell folder). Make sure to modify **basePath** to your repository path."
  55. ]
  56. },
  57. {
  58. "cell_type": "code",
  59. "execution_count": null,
  60. "metadata": {
  61. "dotnet_interactive": {
  62. "language": "csharp"
  63. }
  64. },
  65. "outputs": [],
  66. "source": [
  67. "const string basePath = @\"D:\\GitWorkspace\\ILSpy\\\";\n",
  68. "string testAssemblyPath = basePath + @\"ICSharpCode.Decompiler.PowerShell\\bin\\Release\\netstandard2.0\\ICSharpCode.Decompiler.dll\";\n",
  69. "\n",
  70. "var decompiler = new CSharpDecompiler(testAssemblyPath, new DecompilerSettings());"
  71. ]
  72. },
  73. {
  74. "cell_type": "markdown",
  75. "metadata": {},
  76. "source": [
  77. "Get the count of types in this module"
  78. ]
  79. },
  80. {
  81. "cell_type": "code",
  82. "execution_count": null,
  83. "metadata": {
  84. "dotnet_interactive": {
  85. "language": "csharp"
  86. }
  87. },
  88. "outputs": [
  89. {
  90. "name": "stdout",
  91. "output_type": "stream",
  92. "text": [
  93. "1478\r\n"
  94. ]
  95. }
  96. ],
  97. "source": [
  98. "var types = decompiler.TypeSystem.MainModule.TypeDefinitions;\n",
  99. "Console.WriteLine(types.Count());"
  100. ]
  101. },
  102. {
  103. "cell_type": "markdown",
  104. "metadata": {},
  105. "source": [
  106. "Decompile a known type (as a whole)"
  107. ]
  108. },
  109. {
  110. "cell_type": "code",
  111. "execution_count": null,
  112. "metadata": {
  113. "dotnet_interactive": {
  114. "language": "csharp"
  115. }
  116. },
  117. "outputs": [
  118. {
  119. "name": "stdout",
  120. "output_type": "stream",
  121. "text": [
  122. "using System;\r\n",
  123. "\r\n",
  124. "namespace ICSharpCode.Decompiler.Util\r\n",
  125. "{\r\n",
  126. "\tpublic static class Empty<T>\r\n",
  127. "\t{\r\n",
  128. "\t\tpublic static readonly T[] Array = System.Array.Empty<T>();\r\n",
  129. "\t}\r\n",
  130. "}\r\n",
  131. "\r\n"
  132. ]
  133. }
  134. ],
  135. "source": [
  136. "// ICSharpCode.Decompiler.Util.Empty<T> -> translates to `n, where n is the # of generic parameters\n",
  137. "var nameOfGenericType = new FullTypeName(\"ICSharpCode.Decompiler.Util.Empty`1\");\n",
  138. "Console.WriteLine(decompiler.DecompileTypeAsString(nameOfGenericType));"
  139. ]
  140. },
  141. {
  142. "cell_type": "markdown",
  143. "metadata": {},
  144. "source": [
  145. "If you want to decompile one single member (sample: first method)"
  146. ]
  147. },
  148. {
  149. "cell_type": "code",
  150. "execution_count": null,
  151. "metadata": {
  152. "dotnet_interactive": {
  153. "language": "csharp"
  154. }
  155. },
  156. "outputs": [
  157. {
  158. "name": "stdout",
  159. "output_type": "stream",
  160. "text": [
  161. "using System;\r\n",
  162. "\r\n",
  163. "static UniversalAssemblyResolver()\r\n",
  164. "{\r\n",
  165. "\tgac_paths = GetGacPaths();\r\n",
  166. "\tZeroVersion = new Version(0, 0, 0, 0);\r\n",
  167. "\tif (Type.GetType(\"Mono.Runtime\") != null)\r\n",
  168. "\t{\r\n",
  169. "\t\tdecompilerRuntime = DecompilerRuntime.Mono;\r\n",
  170. "\t}\r\n",
  171. "\telse if (typeof(object).Assembly.GetName().Name == \"System.Private.CoreLib\")\r\n",
  172. "\t{\r\n",
  173. "\t\tdecompilerRuntime = DecompilerRuntime.NETCoreApp;\r\n",
  174. "\t}\r\n",
  175. "\telse if (Environment.OSVersion.Platform == PlatformID.Unix)\r\n",
  176. "\t{\r\n",
  177. "\t\tdecompilerRuntime = DecompilerRuntime.Mono;\r\n",
  178. "\t}\r\n",
  179. "}\r\n",
  180. "\r\n"
  181. ]
  182. }
  183. ],
  184. "source": [
  185. "var nameOfUniResolver = new FullTypeName(\"ICSharpCode.Decompiler.Metadata.UniversalAssemblyResolver\");\n",
  186. "ITypeDefinition typeInfo = decompiler.TypeSystem.FindType(nameOfUniResolver).GetDefinition();\n",
  187. "var tokenOfFirstMethod = typeInfo.Methods.First().MetadataToken;\n",
  188. "Console.WriteLine(decompiler.DecompileAsString(tokenOfFirstMethod));"
  189. ]
  190. },
  191. {
  192. "cell_type": "markdown",
  193. "metadata": {},
  194. "source": [
  195. "If you need access to low-level metadata tables"
  196. ]
  197. },
  198. {
  199. "cell_type": "code",
  200. "execution_count": null,
  201. "metadata": {
  202. "dotnet_interactive": {
  203. "language": "csharp"
  204. }
  205. },
  206. "outputs": [],
  207. "source": [
  208. "ITypeDefinition type = decompiler.TypeSystem.FindType(nameOfUniResolver).GetDefinition();\n",
  209. "var module = type.ParentModule.PEFile;"
  210. ]
  211. },
  212. {
  213. "cell_type": "markdown",
  214. "metadata": {},
  215. "source": [
  216. "Get the child namespaces"
  217. ]
  218. },
  219. {
  220. "cell_type": "code",
  221. "execution_count": null,
  222. "metadata": {
  223. "dotnet_interactive": {
  224. "language": "csharp"
  225. }
  226. },
  227. "outputs": [
  228. {
  229. "name": "stdout",
  230. "output_type": "stream",
  231. "text": [
  232. "Microsoft\r\n",
  233. "System\r\n",
  234. "LightJson\r\n",
  235. "Humanizer\r\n",
  236. "ICSharpCode\r\n",
  237. "FxResources\r\n",
  238. "Internal\r\n",
  239. "MS\r\n",
  240. "Windows\r\n"
  241. ]
  242. }
  243. ],
  244. "source": [
  245. "var icsdns = decompiler.TypeSystem.RootNamespace;\n",
  246. "foreach (var cn in icsdns.ChildNamespaces) Console.WriteLine(cn.FullName);"
  247. ]
  248. },
  249. {
  250. "cell_type": "markdown",
  251. "metadata": {},
  252. "source": [
  253. "Get types in a single namespace"
  254. ]
  255. },
  256. {
  257. "cell_type": "code",
  258. "execution_count": null,
  259. "metadata": {
  260. "dotnet_interactive": {
  261. "language": "csharp"
  262. }
  263. },
  264. "outputs": [
  265. {
  266. "name": "stdout",
  267. "output_type": "stream",
  268. "text": [
  269. "LightJson.JsonArray\r\n"
  270. ]
  271. }
  272. ],
  273. "source": [
  274. "var typesInNamespace = icsdns.ChildNamespaces.First(cn => cn.FullName == \"LightJson\").Types;\n",
  275. "Console.WriteLine(typesInNamespace.First().FullTypeName);"
  276. ]
  277. },
  278. {
  279. "cell_type": "code",
  280. "execution_count": null,
  281. "metadata": {
  282. "dotnet_interactive": {
  283. "language": "csharp"
  284. }
  285. },
  286. "outputs": [],
  287. "source": []
  288. }
  289. ],
  290. "metadata": {
  291. "kernelspec": {
  292. "display_name": ".NET (C#)",
  293. "language": "C#",
  294. "name": ".net-csharp"
  295. },
  296. "language_info": {
  297. "file_extension": ".cs",
  298. "mimetype": "text/x-csharp",
  299. "name": "csharp",
  300. "pygments_lexer": "csharp",
  301. "version": "8.0"
  302. }
  303. },
  304. "nbformat": 4,
  305. "nbformat_minor": 4
  306. }