IL查看override
发布时间:2021-05-21 06:58:32 所属栏目:大数据 来源: https://www.jb51.cc
导读:查看override的IL Override示例 下面我们看一个Override的Example namespace MyCollection { public class MyBase { virtual string Meth1() { return "MyBase-Meth1" ; } ? string Meth2() "MyBase-Meth2" ; string Meth3() "MyBase-Meth3" ; } class MyDer
查看override的ILOverride示例下面我们看一个Override的Example namespace MyCollection
{
public class MyBase {
virtual string Meth1() {
return "MyBase-Meth1"; } ? "MyBase-Meth2"; "MyBase-Meth3"; } // 使用 override 关键字重写虚方法 Meth1: // 使用 new 关键字显式隐藏 new "MyDerived-Meth2";
// 因此将发出一个警告来提醒程序员 "MyDerived-Meth3"; static void Main()
MyDerived mD = new MyDerived();
MyBase mB = (MyBase)mD; System.Console.WriteLine(mD.Meth1()); System.Console.WriteLine(mD.Meth2()); System.Console.WriteLine(mD.Meth3()); System.Console.WriteLine("以上为类MyDerived的显示结果!n");
System.Console.WriteLine(mB.Meth1()); System.Console.WriteLine(mB.Meth2()); System.Console.WriteLine(mB.Meth3()); "按任意键退出..."); System.Console.ReadLine(); } 运行结果
? IL查看双击查看Main方法
IL代码.method public hidebysig void Main() cil managed.entrypoint // Code size 121 (0x79)
.maxstack 1 .locals init ([0] class MyCollection.MyDerived mD,
[1] class MyCollection.MyBase mB)
IL_0000: nop IL_0001: newobj instance void MyCollection.MyDerived::.ctor()
IL_0006: stloc.0 IL_0007: ldloc.0 IL_0008: stloc.1 IL_0009: ldloc.0 IL_000a: callvirt instance string MyCollection.MyBase::Meth1()//调用的是MyBase的Meth1() IL_000f: call void [mscorlib]System.Console::WriteLine(string) IL_0014: nop IL_0015: ldloc.0 IL_0016: callvirt instance string MyCollection.MyDerived::Meth2()
IL_001b: call IL_0021: ldloc.0
IL_0022: callvirt instance string MyCollection.MyDerived::Meth3()
IL_0027: call IL_002d: ldstr bytearray (E5 4E 0A 4E 3A 4E 7B 7C 4D 00 79 00 44 00 65 00 // .N.N:N{|M.y.D.e.
72 00 69 00 76 00 65 00 64 00 84 76 3E 66 3A 79 // r.i.v.e.d..v>f:y
D3 7E 9C 67 01 FF 0A 00 ) // .~.g....
IL_0032: call IL_0038: ldloc.1
IL_0039: callvirt instance //这里一样调用的是MyBase的Meth1()
IL_003e: call IL_0044: ldloc.1
IL_0045: callvirt instance string MyCollection.MyBase::Meth2()
IL_004a: call IL_0050: ldloc.1
IL_0051: callvirt instance string MyCollection.MyBase::Meth3()
IL_0056: call IL_005c: ldstr bytearray (E5 4E 0A 4E 3A 4E 4D 00 79 00 42 00 61 00 73 00 // .N.N:NM.y.B.a.s.
65 00 84 76 3E 66 3A 79 D3 7E 9C 67 01 FF 0A 00 ) // e..v>f:y.~.g....
IL_0061: call IL_0067: ldstr bytearray (09 63 FB 4E 0F 61 2E 95 00 90 FA 51 2E 00 2E 00 // .c.N.a.....Q....
2E 00 ) // ..
IL_006c: call IL_0072: call string [mscorlib]System.Console::ReadLine()
IL_0077: pop IL_0078: ret } // end of method VirtualExample::Main
? IL解析我们先回头看看源程序处的main内部的“System.Console.WriteLine(mD.Meth1());”语句对应上面的 IL_000a:? callvirt?? instance string MyCollection.MyBase::Meth1() 和 IL_0039:? callvirt?? instance string MyCollection.MyBase::Meth1()处的一模一样,原来它执行的是MyBase类的Meth1虚方法。而Meth1方法已经在 MyDerived类中重写了,所以这两个类的对应的方法1在本质说上都一样了。 通过看这个例子,我们能更加深入地理解override的功能了。看来ILdasm的确厉害,与其你想半天不如调IL代码看看,很多问题就会迎刃而解了,拨云见日啊! 文献资料对于IL代码指令的具体含义请参考:http://www.cnblogs.com/zery/p/3366175.html (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |


