'ByRef'参数'<parametername>'不能用于lambda表达式</parametername>

时间:2011-02-12 16:41:46

标签: vb.net lambda pass-by-reference byref

我正在使用SharpZipLib来压缩文件。该库包含在一个插件接口中,位于一个单独的DLL中。我将插件dll传递给ByRef参数以跟踪压缩进度。

SharpZipLib在压缩时会定期调用启动压缩时传递的委托子。在调用委托时,我无法弄清楚如何更新ByRef参数。如果我尝试在lamba表达式的主体中分配ByRef变量,则会出现'ByRef' parameter '<parametername>' cannot be used in a lambda expression错误。

这是我的代码:

Using InputFile As New IO.FileStream(SourceFile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    Using OutputFile As New IO.FileStream(DestFile, IO.FileMode.Create)
        Using GZipStream As New GZipOutputStream(OutputFile)
            Dim Buffer(524228) As Byte
            Dim Handler As New ProgressHandler(Sub(Sender As Object, EventArgs As ProgressEventArgs) Progress += EventArgs.Processed)
            StreamUtils.Copy(InputFile, GZipStream, Buffer, Handler, New TimeSpan(10000000), Nothing, "")
        End Using
    End Using
End Using 

谢谢!

1 个答案:

答案 0 :(得分:0)

无论您是否使用匿名,都无法使用ByRef参数( ref out 在C#中)声明 Sub 委托功能与否。

但是你可以声明你的委托类型,然后使用你的匿名函数

MSDN上,它提到以下规则适用于lambda表达式中的变量范围:

  • 在引用它的委托超出范围之前,捕获的变量不会被垃圾收集。
  • 在lambda表达式中引入的变量在外部方法中不可见。
  • lambda表达式不能直接捕获ref [ByRef in VB]或out参数来自封闭方法。
  • lambda表达式中的return语句不会导致封闭方法返回。
  • lambda表达式不能包含goto语句,break语句或continue语句,其目标位于正文之外或包含匿名函数的正文中。
相关问题