“通过参考”参数 '<参数名称>'不能在 lambda 表达式中使用参数名称>
我正在使用 SharpZipLib 来压缩文件。该库封装在一个插件接口中,位于一个单独的 DLL 中。我向插件 dll 传递一个 ByRef
参数来跟踪压缩进度。
SharpZipLib 在压缩时将定期调用启动压缩时传递的委托子。我不知道如何在调用委托时更新 ByRef
参数。如果我尝试在lambda表达式主体中分配ByRef
变量,我会得到一个'ByRef'参数'
错误。
这是我的代码:
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
谢谢!
I'm using SharpZipLib to compress files. The library is wrapped in a plugin interface, in a separate DLL. I pass the plugin dll a ByRef
parameter to keep track of the compression progress.
SharpZipLib, while compressing, will periodically call a delegate sub passed when launching the compression. I can't figure out how to update the ByRef
parameter when the delegate is called. If I try to assign the ByRef
variable in the body of a lamba expression, I get a 'ByRef' parameter '<parametername>' cannot be used in a lambda expression
error.
Here's my code:
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
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论您是否使用匿名函数,都不能使用 ByRef 参数(C# 中的 ref 或 out)声明 Sub 委托。
但是您可以声明您的委托类型,然后将其与您的匿名函数一起使用
MSDN< /a> 它提到以下规则适用于 lambda 表达式中的变量范围:
You can't declare the Sub delegate with ByRef parameters (ref or out in C#), no matter if you using an anonymous function or not.
But you can declare your delegate type and then use it even with your anonymous function
On MSDN it mentions the following rules apply to variable scope in lambda expressions:
我知道这个问题已经有 4 年历史了,但我也面临着同样的问题,并且我找到了答案,所以我想与您分享解决方案。
根据 Microsoft 在 MSDN 上的回答页:
希望答案对任何人有帮助。
I know that question is 4 years old but i'm just facing the same problem and i figured it out so i want to share the solution with you.
According to the Microsoft answer on the MSDN page:
Hope the answer help anyone.