大佬教程收集整理的这篇文章主要介绍了curry委托参数的最佳模式是什么(使用.NET 2.0或更高版本)?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
可以在VB 2010中使用lambda表达式,但lambda表达式与edit-and-conTinue不兼容,并且它们创建的闭包可以具有意外的副参考行为.另一种方法是定义一些通用方法,如下所示:
Public Module Currymagic Delegate Sub Action(Of T1,T2)(ByVal P1 As T1,ByVal P2 As T2) Delegate Sub Action(Of T1,T2,T3)(ByVal P1 As T1,ByVal P2 As T2,ByVal P3 As T3) Class CurriedAction0(Of FixedType1,FixedType2) Dim _theAction As Action(Of FixedType1,FixedType2) Dim _FixedVal1 As FixedType1,_FixedVal2 As FixedType2 Sub Exec() _theAction(_FixedVal1,_FixedVal2) End Sub Sub New(ByVal theAction As Action(Of FixedType1,FixedType2),_ ByVal FixedVal1 As FixedType1,ByVal FixedVal2 As FixedType2) _theAction = theAction _FixedVal1 = FixedVal1 _FixedVal2 = FixedVal2 End Sub End Class Class CurriedAction1(Of ArgType1,FixedType1,FixedType2) Dim _theAction As Action(Of ArgType1,_FixedVal2 As FixedType2 Sub Exec(ByVal ArgVal1 As ArgType1) _theAction(ArgVal1,_FixedVal1,_FixedVal2) End Sub Sub New(ByVal theAction As Action(Of ArgType1,ByVal FixedVal2 As FixedType2) _theAction = theAction _FixedVal1 = FixedVal1 _FixedVal2 = FixedVal2 End Sub End Class Class ActionOf(Of ArgType1) Shared Function Create(Of FixedType1,FixedType2)(ByVal theSub As Action(Of ArgType1,ByVal FixedVal1 As FixedType1,ByVal FixedVal2 As FixedType2) As Action(Of ArgType1) Return AddressOf New CurriedAction1(Of ArgType1,FixedType2)(theSub,FixedVal1,FixedVal2).Exec End Function End Class Function NewInvoker(Of FixedType1,FixedType2)(ByVal theSub As Action(Of FixedType1,ByVal FixedVal2 As FixedType2) As MethodInvoker Return AddressOf New CurriedAction0(Of FixedType1,FixedVal2).Exec End Function End Module
如果我想创建一个将执行Foo(5,“Hello”)的MethodInvoker,我可以创建一个使用
@H_969_3@myInvoker = NewInvoker(AddressOf Foo,5,"Hello")如果我想将MyAction(X)转换成Boz(X,“George”,9),其中x是Double,我可以使用
@H_969_3@myAction = ActionOf(Of DoublE).Create(AddressOf Boz,"George",9)除了必须有大量的样板代码以适应不同数量的固定和非固定参数之外,所有这些都很漂亮,而委托创建语法中没有什么固有的,这清楚了哪些参数是固定的,固定.有没有办法改善模式?
附录:
如果委托是从一个struct成员函数创建的,那么机制是什么?看来,委托获取自己的结构体副本,但是我不知道该副本是否装箱或取消装箱.如果没有装箱,则用结构体替换CurryAction0和CurryAction1将避免在创建委托时将CurryAction0或CurryAction1分配为单独的堆对象.如果将要装箱,那么使用一个结构体会增加将结构复制到盒装实例的开销,而不保存任何东西.
''Create new tuple instance with two items. Dim tuple As Tuple(Of Integer,String) = _ New Tuple(Of Integer,String)(5,"Hello") ''Now you only have one argument to curry,packaging both parameters ''Access the parameters like this (strongly typed) Debug.Print tuple.Item1 '' 5 Debug.Print tuple.Item2 '' "Hello"
以上是大佬教程为你收集整理的curry委托参数的最佳模式是什么(使用.NET 2.0或更高版本)?全部内容,希望文章能够帮你解决curry委托参数的最佳模式是什么(使用.NET 2.0或更高版本)?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。