C#ã§ä½ãããããã°ã©ã ããã³ã³ãã¤ã«ãã¦ã¿ãã
ãã®è¨äºã¯C#ã¢ããã³ãã«ã¬ã³ãã¼2014ã®15æ¥ç®ã®è¨äºã§ãã
C# Advent Calendar 2014 - Qiita
ãã³ã³ãã¤ã«ã¨ã¯
大éæã«è¨ãã¨ãã³ã³ãã¤ã«ãããããã°ã©ã (exe, dllãªã©)ãããå ã®ã³ã¼ãã復å ããè¡çºã®ãã¨ã§ãã
ç¹ã«C#ãJavaã®æ§ã«ä¸éè¨èªã«ç¿»è¨³ãããç¶æ
ã§ã³ã³ãã¤ã«ããããããªè¨èªã®å ´åã¯
å
ã®ã³ã¼ãã«ããªãå¿ å®ã«å¾©å
ããã¾ãã
æ¬è¨äºã§ã¯C#ã«éããã.NETã«é¢ããè¨èªå ¨è¬ã®ãã³ã³ãã¤ã«ãããæ¹æ³ãæ¸ãã¦ããã¾ãã*1*2
ä¸éè¨èª(IL)
C#(ããVB.NETãªã©)ã§ã¯ãã³ã³ãã¤ã«æã«ç´æ¥ãã¤ãã£ãã³ã¼ãã«ããã®ã§ã¯ãªãã
ä¸æ¦ä¸éè¨èª(以ä¸ãIL)ã¨ããå½¢ã«ããå®è¡æã«ãã¤ãã£ãã³ã¼ããçæããJITã³ã³ãã¤ã«æ¹å¼ãæ¡ç¨ãã¦ãã¾ãã
äºåã³ã³ãã¤ã«ã¨ããæ段ãç¨æããã¦ãã¾ããåºæ¬çã«ã¯ILãåºåãã¾ãã
ã¨ããã§ããã¤ãã£ãã³ã³ãã¤ã«ãããªãã¨ãããã¨ã¯ãJITã³ã³ãã¤ã©ãåå¨ããç°å¢ã§ããããã°ã
å®è¡ããç°å¢ãé¸ã°ãªãã¨ããã¡ãªãããããã¾ãã
ä»ã¾ã§ã¯Windowsç°å¢ã§å®è¡ããããã¨ãæ®ã©ã ã£ãã®ã§ãã¾ãæ°ã«ããããæ©ä¼ã¯ããã¾ããã§ãããã
æè¿ã®Dockeréçãç¸ã¾ã£ã¦ãLinuxç°å¢ã§å®è¡ããæ©ä¼ãå¢ãã¦ããã
éã«JITã³ã³ãã¤ã«å½¢å¼ã®å¼·ã¿ãæ¬å½ã®æå³ã§çãããããã¨ã«ãªãããã§ãã
é話ä¼é¡ãILã¯ãã¤ãã³ã¼ãã®ããããã®ã¾ã¾ã§ã¯äººéãèªãäºãã§ãã¾ãããã
Visual Studioã«ä»å±ãã¦ããildasm.exeã使ããã¨ã§ã¢ã»ã³ããªè¨èªãé«ç´ã«ãããããªå½¢å¼ã§ç¢ºèªãããã¨ãã§ãã¾ãã
éã¢ã»ã³ãã«ï¼IL(ãã¤ãã³ã¼ã)ãã人ãèªããå½¢ã®ILã¸
æ¬é¡ã®ãã³ã³ãã¤ã«ã«å ¥ãã¾ãã ã¾ãã¯IL(ãã¤ãã³ã¼ã)ãã人ãèªããå½¢ã«åºåãããéã¢ã»ã³ãã«ã«ã¤ãã¦ã®ç´¹ä»ã§ãã
ildasm.exe
å
ã»ã©æããVisual Studioã«ä»å±ãã¦ããéã¢ã»ã³ãã©ã¼ã
dllãexeã«å¯¾ãã¦ä½¿ç¨ããäºã§ILãããã¹ãã¨ãã¦åºåã§ãã¾ãã
ildasm.exeã¯SDKå ã«åå¨ãã¦ãã¾ãã
C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin C:\Program Files\Microsoft SDKs\Windows\v7.0A\x64\bin C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\x64\bin
試ãã«èµ·åãã¦ã¿ãã¨ãã®ãããªã¦ã£ã³ãã¦ã表示ããã¾ãã
ä»åã¯GUIã§æä½ããã¾ãããã³ãã³ãã©ã¤ã³ãããå®è¡ã§ããã®ã§ãä¸æ¬ã§å¦çãããå ´åãªã©ã¯ãã¡ãããå©ç¨ãã ããã
Ildasm.exe (IL 逆アセンブラー) | Microsoft Docs
ããã§ã¯è©¦ãã«é©å½ãªã³ã¼ããæ¸ãã¦ã³ã³ãã¤ã«ãããã®ãildasm.exeã«éãã¦ã¿ã¾ãããã
namespace Hogehogee { public class SampleClass { public string HelloHogehoge(string[] args) { var word = args[0]; return string.Format("Hello {0}", word); } } }
ãããä½æãã¦Debugãã«ããçæãããdllãå ã»ã©ã®ã¦ã£ã³ãã¦ã§éãã¦ã¿ã¾ãããã
ããã¨ä»¥ä¸ã®æ§ã«å±éããã¾ãã
ä¸ããé ã«åå空éãã¯ã©ã¹åãã¯ã©ã¹æ
å ±(.class)ãã³ã³ã¹ãã©ã¯ã¿æ
å ±(.ctor)ãHelloHogehogeã¡ã½ããã®æ
å ±ã§ãã
ãããããããã«ã¯ãªãã¯ãããã¨ã§ä¸èº«ãããã£ã¨è¦ããã¨ãã§ãã¾ãã*3
ã¯ã©ã¹åã®é ä¸ã«ããæ å ±ã«ã¤ãã¦è¦ã¦ããã¾ãã
ã¾ãã¯.classã
.class public auto ansi beforefieldinit Hogehogee.SampleClass extends [mscorlib]System.Object { } // end of class Hogehogee.SampleClass
æ®æ®µãã¾ãæ°ã«ãã¾ããããã¡ããã¨ä»æ§éãSystem.Objectãæé»çã«ç¶æ¿ããã¦ããäºãè¦ã¦åãã¾ãã
次ã«.ctorã
.method public hidebysig specialname rtspecialname instance void .ctor() cil managed { // ã³ã¼ã ãµã¤ãº 7 (0x7) .maxstack 8 IL_0000: ldarg.0 IL_0001: call instance void [mscorlib]System.Object::.ctor() IL_0006: ret } // end of method SampleClass::.ctor
ãã¡ããæé»çã«åºåºã¯ã©ã¹(System.Object)ã®ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãå¼ã°ãã¦ããäºããªãã¨ãªããããã¾ãã
C#ã®ä»æ§ã§ãã¹ã¼ãã¼ã¯ã©ã¹ã®ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãæé»çã«å¼ã°ããã¨ããä»æ§éãã§ãã
æå¾ã«HelloHogehogeã¡ã½ããã
.method public hidebysig instance string HelloHogehoge(string[] args) cil managed { // ã³ã¼ã ãµã¤ãº 21 (0x15) .maxstack 2 .locals init ([0] string word, [1] string CS$1$0000) IL_0000: nop IL_0001: ldarg.1 IL_0002: ldc.i4.0 IL_0003: ldelem.ref IL_0004: stloc.0 IL_0005: ldstr "Hello {0}" IL_000a: ldloc.0 IL_000b: call string [mscorlib]System.String::Format(string, object) IL_0010: stloc.1 IL_0011: br.s IL_0013 IL_0013: ldloc.1 IL_0014: ret } // end of method SampleClass::HelloHogehoge
ã¡ããã¨ããã³ã¼ããæ¸ãããã¨ããILã«ã¤ãã¦ã¡ããã¨ç¥ããªãã¨ä½ãä½ããã
ããããªãã¨ãªã"Hello {0}"æååã使ã£ã¦string.Format()ãå¼ã³åºãã¦ããæ§ã«è¦ãã¾ãã
ããã¦ãªãã¨ãªãã§ããwordã£ã¦å¤æ°ã£ã½ããã®ãè¦ãã¾ãã*4
ãªãã¨ãªã¼ããããã¾ããããã ãC#ã®ã³ã¼ãããã¯ããªãç¨é ããã®ãåºã¦ãã¾ãã
ILã®ä»æ§ãç解ããªãã¨ããã£ãããã ãã§ãå²ã¨åè¦å
«è¦ãã¦ãã¾ãããã§ãã
å®éã®ãããã¯ãã®ã³ã¼ããå
¨é¨ILã§èªããã¨ãããè¾ãããããã¾ããã
ããããå¾è¿°ããããã«ãC#ã®ã³ã¼ããã®ãã®ã復å
ãããã³ã³ãã¤ã©ãéçºããã¦ãã¾ãã®ã§ã
ã³ã¼ãããã¯èªã¿åããªããããªè¬ã®åãã解æããå ´å以å¤ã¯ãã¡ãã使ããã¨ã¯ãã¾ãç¡ãããããã¾ããã
LINQPad
å°ã趣åã¯éãã¾ãããLINQPadã§ã人ãèªããå½¢å¼ã®ILã確èªãããã¨ãã§ãã¾ãã
ãã¡ãã¯dllãexeããã³ã³ãã¤ã«ããã®ã§ã¯ãªããã³ã¼ããæ¸ãã¦ãããã«ãã£ã¦çæãããILã確èªããäºãã§ãã¾ãã
å½ããã°ã§ãéå»ã«å°ãã ã使ã£ã¦ããã®ã§ãã¡ããè¦ã¦é ããã°ã¨æãã¾ãã
Itemã¯ã©ã¹ã«Indexerãåå¨åºæ¥ãªãçç±ãILãçºããã¨ãããã¾ãã*5
C#における Indexer ちょっと特殊なケースについて - ほげほげー
ILã確èªãã以å¤ã®è¶
絶便å©ãªä½¿ãæ¹ã¯ã¢ããã³ãã«ã¬ã³ãã¼ 6æ¥ç®ã®
takeshik ãããã¼ã®è¨äºããåç
§ãã ããã
今日からはじめる LINQPad | TAKESHIK.ORG
ãã³ã³ãã¤ã«ï¼IL(ãã·ã³èª)ããå ã®ã³ã¼ãã復å
ãã¦ããããããæ¬è¨äºã®æ¬é¡ã§ãã
åè¿°ã®ããã«ã人ã«èªããå½¢ã§ILãåºåãã¦ãããã®ãããã¯ããã§ä¾¿å©ã§ããã
ãã¯ãé常ã®C#ã®ã³ã¼ãã¨ãã¦èªããã»ããæ©ããå¬ããã§ãããã
ããã§ãILã解éãã¦C#ã®ã³ã¼ããåºåãã¦ãããç´ æµãªãã³ã³ãã¤ã©ã®åºçªã§ãã
.NET Reflector ã«å§ã¾ã dotPeek, ILSpy, etc...
大ä½ã®å ´åã¯ãããã使ããã¨ã§ããªãèªã¿ãããã³ã¼ããåºåããã¾ãã
å¤æ°åã®æ
å ±(誤ã£ã¦Debugãã«ãã§æä¾ããã¦ããªãéã)ã¯æ®ã£ã¦ããªãã®ã§ã
str1, str2ãªã©ã¨ãªãã¾ãããããã¯ä»æ¹ãªãã®ã§ææ
¢ãã¾ãããã
以ä¸ã«ããã¤ãã®ãã³ã³ãã¤ã©ãè¨è¼ãã¾ãã®ã§ãè²ã 試ãã¦ã¿ã¦é ããã°ã¨æãã¾ãã
.NET Reflector
以åã¯ãã¡ããç¡åæä¾ããã¦ããã®ã§ããä¸æã§ããã
è±å¯ãªãã©ã°ã¤ã³ããããä»ã§ãæ好家ãå¤ãããã§ãã
2011å¹´ã®3æã«æååãããæ°è»½ã«ã¯ä½¿ããªããªãã¾ãããã
ãã®éããã¤ãã®ç«¶åãããã¯ããç¡ååãå
¬éããã¾ããã
以ä¸ã«ããã競åãããã¯ããæãã¦ããã¾ãã
dotPeek
dotPeek: Free .NET Decompiler & Assembly Browser by JetBrains JetBrains社製ã®ãã³ã³ãã¤ã©ã ReSharperã«ä»å±ãã¦ãã¦ãå®ç¾©ã¸ç§»åããéã«dotPeekãéãã¦ãã³ã³ãã¤ã«ãããã®ã表示ããããã¦ããã¾ãã
UIãVisual Studioã©ã¤ã¯ã§ããªã馴æã¿ãããã®ã§ã¯ãªãã§ããããã
ILSpy
MITã©ã¤ã»ã³ã¹ã§å ¬éããã¦ãããªã¼ãã³ã½ã¼ã¹ã§ãã
ã¹ã¿ã³ãã¢ãã³ã§å©ç¨ã§ãããããæ®æ®µã¯ãã£ã±ããã¡ããå©ç¨ãã¦ãã¾ãã
試ãã«æåã«ä½ã£ãdllãããã使ã£ã¦ãã³ã³ãã¤ã«ããçµæãè¼ãã¦ããã¾ãã
// Debug ãã«ãç using System; namespace Hogehogee { public class SampleClass { public string HelloHogehoge(string[] args) { string word = args[0]; return string.Format("Hello {0}", word); } } }
// Release ãã«ãç(pdbãã¡ã¤ã«æ) using System; namespace Hogehogee { public class SampleClass { public string HelloHogehoge(string[] args) { string word = args[0]; return string.Format("Hello {0}", word); } } }
// Release ãã«ãç(pdbãã¡ã¤ã«ç¡) using System; namespace Hogehogee { public class SampleClass { public string HelloHogehoge(string[] args) { string arg = args[0]; return string.Format("Hello {0}", arg); } } }
Release ãã«ãã§pdbãã¡ã¤ã«ãåé¤ããããã®ä»¥å¤ã§ã¯æé»çã«ä»ä¸ãããusing System;ãæ示ãããã ãã§
æåã«æ¸ãããã®ã¨ã»ã¼åçã®ãã®ãåºåããã¾ããã
é常ã«ä¾¿å©ã§ããã
ããã¾ã§è¦äºã«æ»ãããã¨ãã»ã³ã·ãã£ããªæ
å ±ãã³ã¼ãå
ã«åãè¾¼ããã¨ã¯é常ã«æãã
ã¨ããæã容æã«ç解ã§ãããã¨æãã¾ãã
ããã°ã©ã ãæä¾ããéã«ã¯ãæ°ãã¤ãä¸ããã
ãã®ä»
JustDecompile .NET Assembly Decompiler & Browser - Telerik
CodeReflect - Free .NET Decompiler
ã¾ã¨ã
ãªã¼ãã³ã½ã¼ã¹ãªãããã¯ããå¢ãã¦ãã¦ããã®ã§æ´»ç¨ã®æ©ä¼ã¯å¤å°æ¸ã£ã¦ãã¾ããã
ã¾ã ã¾ã .NETã§ã¯ãã¼ãºããªãããã¯ããæ±ããã¨ã¯å¤ã
ããã¾ããããããã®ã»ããå¤ãã§ãããã
ãã®éã«ã¯è¦ç´ãè¯ãèªã¿ãåé¡ãç¡ããã°ãããããã使ã£ã¦ããã¾ãããã
åå¼·ã«ããªãã¾ãããã
ä»åã¯åãä¸ãã¦ãã¾ããããVB.NETã®çæç©ããC#ãC#ã®çæç©ããVB.NETãF#ã®çæç©ããC#ãªãã¦ãã¨ãåºæ¥ã¾ãã®ã§ã
èå³ãããã°ã試ããã ããã
F#ãC#ãVB.NETã«å¤æããã¨ãF#ãã©ã®ããã«ãã¦ãã¿ã¼ã³ããããå®ç¾ãã¦ããã®ããåãã£ãããã¦é¢ç½ãã§ããã
ææ¥ã¯ãoikaãããã§ããããããï¼
*1:ãã³ã³ãã¤ã«ã«ãããªãã¼ã¹ã¨ã³ã¸ãã¢ãªã³ã°ãç¦æ¢ããã¦ããã¢ããªã±ã¼ã·ã§ã³ã«ã¯ä½¿ç¨ããªãããã«ãã¾ããã
*2:EUã§ã¯æ³çã«ä½ããããã¨ãã³ã³ãã¤ã«OKãããï¼ éã³ã³ãã¤ã© - Wikipedia http://ja.wikipedia.org/wiki/%E9%80%86%E3%82%B3%E3%83%B3%E3%83%91%E3%82%A4%E3%83%A9
*3:DUMPãã§ãã¾ããä»åã¯åãä¸ãã¾ãã
*4:ä»åã¯Debugãã«ãã ã£ãã®ã§ã¡ã½ããå ã§å®£è¨ããå¤æ°åã®æ å ±ãæ®ã£ã¦ãã¾ããèå³ãããæ¹ã¯è©¦ãã«Releaseãã«ãã«ãã¦çµæã®éãã確èªãã¦ã¿ã¦ãã ããã