WEB开发网
开发学院软件开发C语言 [C# 4.0] 1. Dynamic 阅读

[C# 4.0] 1. Dynamic

 2010-09-30 21:04:24 来源:WEB开发网   
核心提示: !do 00d3caf8 // TargetName:System.Action`2[[System.Runtime.CompilerServices.CallSite,System.Core],[System.Object,mscorlib]]MethodTable:60fa9208EECla

!do 00d3caf8 // Target

Name: System.Action`2[[System.Runtime.CompilerServices.CallSite, System.Core],[System.Object, mscorlib]]
MethodTable: 60fa9208
EEClass: 7990a4fc
Size: 32(0x20) bytes
File: C:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
Fields:
 MT Field Offset Type VT Attr Value Name
79bdbcb8 4000072 4 System.Object 0 instance 00d3a3f4 _target
79bdbcb8 4000073 8 System.Object 0 instance 00d34d10 _methodBase
79bb42b4 4000074 c System.IntPtr 1 instance 034D00B0 _methodPtr
79bb42b4 4000075 10 System.IntPtr 1 instance 00000000 _methodPtrAux
79bdbcb8 4000076 14 System.Object 0 instance bbe3d13300000000 _invocationList
79bb42b4 4000077 18 System.IntPtr 1 instance 00000000 _invocationCount

!ip2md 034D00B0 // _methodPtr

MethodDesc: 00a16a34
Method Name: DynamicClass.CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Object)
Class: 00a16988
MethodTable: 00a169f4
mdToken: bbe3cccb06000001
Module: 00a16444
IsJitted: yes
CodeAddr: 034d00b0
Transparency: Transparent

!dumpil 00a16a34

This is dynamic IL. Exception info is not reported at this time.
If a token is unresolved, run "!do <addr>" on the addr given
in parenthesis. You can also look at the token table yourself, by
running "!DumpArray 00d3ca5c".
IL_0000: ldarg.2 
IL_0001: brfalse IL_0020
IL_0006: ldarg.2 
IL_0007: callvirt 6000003 
IL_000c: ldarg.0 
IL_000d: ldfld 4000004 (00d39f54)
IL_0012: ldc.i4.0 
IL_0013: ldelem.ref 
IL_0014: castclass 2000005 
IL_0019: ceq 
IL_001b: br IL_0021
IL_0020: ldc.i4.0 
IL_0021: brfalse IL_0032
IL_0026: ldarg.2 
IL_0027: castclass 2000006 
IL_002c: callvirt 6000007 Learn.CUI.MyClass.Test()
IL_0031: ret 
IL_0032: ldarg.1 
IL_0033: call 6000008 
IL_0038: brfalse IL_003e
IL_003d: ret 
IL_003e: ldarg.1 
IL_003f: castclass 2000009
IL_0044: callvirt 600000a 
IL_0049: ldarg.1 
IL_004a: ldarg.2 
IL_004b: callvirt 600000b 
IL_0050: ret 

反编译的 IL Metadata Token 貌似有点不对劲。(是 SOS.dll 没完工,还是 CLR 4.0 Metadata Token 升级到 64 位了?Beta2, v4.0.21006, SOS.dll v4.0.21006.1)

虽然 IL 代码有点乱,不过凑活着还是能看个大概的。

从 MSDN 的介绍上来看,dynamic 是为了和 DLR Object 进行交互,这倒也能理解,毕竟受 C# 静态语言调用规则的限制,我们只能用复杂的 Invoke(name) 模式,很显然不利于提升开发效率和简化代码维护。不过 dynamic 同样也带来重构等方面的问题,这也需关注。个人觉得如果没有必要的话,还是尽量少用吧。对于那些需要做反射调用的地方,可以考虑一下,但也尽可能局限于某个 Inner Block,dynamic object 最好也局限于某个有限的 Context 之中。

dynamic 一个非常有用的作用就是 UnitTest,如此我们可以事先写好调用逻辑,即便我们从没实现它,也一样可以编译。

上一页  3 4 5 6 7 8 

Tags:Dynamic

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接