将Sub包装为函数以在Lambda中使用 [英] Wrap Sub as Function for use in Lambda
问题描述
我对VB9和Moq有问题.
I have a problem with VB9 and Moq.
我需要在Sub上调用验证.像这样:
I need to call a verify on a Sub. Like so:
logger.Verify(Function(x) x.Log, Times.AtLeastOnce)
我的记录器如下:
Public Interface ILogger
Sub Log()
End Interface
但是对于VB,这是不可能的,因为Log方法是Sub,因此不会产生值.
But with VB this is not possible, because the Log method is a Sub, and thereby does not produce a value.
我不想将方法更改为函数.
I don't want to change the method to be a function.
解决此限制的最干净的方法是什么?有什么方法可以像下面这样将Sub作为函数包装?
Whats the cleanest way of working around this limitation and is there any way to wrap the Sub as a Function like the below?
logger.Verify(Function(x) ToFunc(AddressOf x.Log), Times.AtLeastOnce)
我已经尝试过了,但是我得到了:
I have tried this, but i get:
Lambda参数不在范围内
Lambda Parameter not in scope
推荐答案
VB10允许使用Lambada Subs.
VB10 allows for the usage of Lambada Subs.
您是否尝试过一个简单的包装器,例如:
Have you tried a simple wrapper, such as:
Public Function Wrapper(source as Action) as Boolean
source.Invoke()
Return True
End Function
这篇关于将Sub包装为函数以在Lambda中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!