您的位置:首页 > 其它

深入了解Dynamic & DLR(一)

2011-09-21 16:46 246 查看
本文主要谈谈个人对DLR的认识和学习,希望能和大家一起探讨下。

主要内容:

DLR在面向服务设计的必要性

dynamic类型

DLR ScriptRuntime

DynamicObject & ExpandoObject

1.DLR在面向服务设计中的必要性

题外前因话:商业业务环境的变更总是伴随着技术的变革。近来面向服务编程SOA的兴起是一种必然,涉及以下几个因素:

其一商业经营需要及时快速的调整;

其二在快速调整的同时,无需额外的开销,无需重构代码,重新发布平台;

其三商业平台的扩展性和稳定性;

其四商业平台维护费用;

SOA在业务平台整体上进行布局整合,发布服务契约,数据契约等等,那么涉及细颗粒度的操作,比如说商场需要实时对销货策略进行变更。1.客户类别不同打折销售,2.节假日不同打折,3.不同的商品类别折扣不同,4.促销商品折扣,5.销售额满多少,折扣多少等等,我们需要以后对销售策略进行实时维护,能新增,删除,修改。在当前我们可能通过App.config文件设置不同参数值来满足我们的业务需求,或者通过dll反射的方式来选择不同销售策略。但这些能灵活配置的有限,无法满足日益增长复杂业务逻辑的变化。对此我们需要这些业务需求能够“动态加载,动态注入”,最好是脚本语言,不需要进行第二次编译,现有的Framework能够自动解析执行。

C#仍是一种静态的类型化语言,无法完美支持动态编程。而Ruby,Python,JavaScript等天生在动态编程方面有绝对的优势。如果能加载第三方动态语言,并且自身具备和这些动态语言相同的某些动态功能。DLR(Dynamic Language Runtime)是添加到CLR的一系列服务,具有动态运行功能,并且允许加载第三方动态语言脚本,如Ruby和Python。DLR体系结构图如下:

View Code

.method private hidebysig static void  Main(string[] args) cil managed
{
.entrypoint
// Code size       125 (0x7d)
.maxstack  8
.locals init ([0] class Loong.Dynamic.DynamicUser dynamicUser,
[1] class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo[] CS$0$0000)
IL_0000:  nop
IL_0001:  newobj     instance void Loong.Dynamic.DynamicUser::.ctor()
IL_0006:  stloc.0
IL_0007:  ldsfld     class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Action`3<class [System.Core]System.Runtime.CompilerServices.CallSite,class [mscorlib]System.Type,object>> Loong.Dynamic.Program/'<Main>o__SiteContainer0'::'<>p__Site1'
IL_000c:  brtrue.s   IL_0051
IL_000e:  ldc.i4     0x100
IL_0013:  ldstr      "WriteLine"
IL_0018:  ldnull
IL_0019:  ldtoken    Loong.Dynamic.Program
IL_001e:  call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_0023:  ldc.i4.2
IL_0024:  newarr     [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo
IL_0029:  stloc.1
IL_002a:  ldloc.1
IL_002b:  ldc.i4.0
IL_002c:  ldc.i4.s   33
IL_002e:  ldnull
IL_002f:  call       class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo::Create(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags,
string)
IL_0034:  stelem.ref
IL_0035:  ldloc.1
IL_0036:  ldc.i4.1
IL_0037:  ldc.i4.0
IL_0038:  ldnull
IL_0039:  call       class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo::Create(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfoFlags,
string)
IL_003e:  stelem.ref
IL_003f:  ldloc.1
IL_0040:  call       class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::InvokeMember(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags,
string,
class [mscorlib]System.Collections.Generic.IEnumerable`1<class [mscorlib]System.Type>,
class [mscorlib]System.Type,
class [mscorlib]System.Collections.Generic.IEnumerable`1<class [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo>)
IL_0045:  call       class [System.Core]System.Runtime.CompilerServices.CallSite`1<!0> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Action`3<class [System.Core]System.Runtime.CompilerServices.CallSite,class [mscorlib]System.Type,object>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder)
IL_004a:  stsfld     class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Action`3<class [System.Core]System.Runtime.CompilerServices.CallSite,class [mscorlib]System.Type,object>> Loong.Dynamic.Program/'<Main>o__SiteContainer0'::'<>p__Site1'
IL_004f:  br.s       IL_0051
IL_0051:  ldsfld     class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Action`3<class [System.Core]System.Runtime.CompilerServices.CallSite,class [mscorlib]System.Type,object>> Loong.Dynamic.Program/'<Main>o__SiteContainer0'::'<>p__Site1'
IL_0056:  ldfld      !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Action`3<class [System.Core]System.Runtime.CompilerServices.CallSite,class [mscorlib]System.Type,object>>::Target
IL_005b:  ldsfld     class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Action`3<class [System.Core]System.Runtime.CompilerServices.CallSite,class [mscorlib]System.Type,object>> Loong.Dynamic.Program/'<Main>o__SiteContainer0'::'<>p__Site1'
IL_0060:  ldtoken    [mscorlib]System.Console
IL_0065:  call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
IL_006a:  ldloc.0
IL_006b:  ldfld      object Loong.Dynamic.DynamicUser::Nickname
IL_0070:  callvirt   instance void class [mscorlib]System.Action`3<class [System.Core]System.Runtime.CompilerServices.CallSite,class [mscorlib]System.Type,object>::Invoke(!0,
!1,
!2)
IL_0075:  nop
IL_0076:  call       string [mscorlib]System.Console::ReadLine()
IL_007b:  pop
IL_007c:  ret
} // end of method Program::Main


由此可见,编译器需要做的优化工作是很多的。复杂的操作也会带来效能上可见的损耗。

在下一篇文章中我会继续和大家探讨下DLR 和常用的两个类DynamicObject、ExpandoObject。

文章首发站点:www.agilesharp.com IT创业产品互推平台
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: