开发者

Wrap Sub as Function for use in Lambda

开发者 https://www.devze.com 2022-12-24 10:42 出处:网络
I have a problem with VB9 and Moq. I need to call a verify on a Sub. Like so: logger.Verify(Function(x) x.Log, Times.AtLeastOnce)

I have a problem with VB9 and Moq.

I need to call a verify on a Sub. Like so:

logger.Verify(Function(x) x.Log, Times.AtLeastOnce)

And my logger looks like this:

Public Interface ILogger
    Sub Log()
End Interface

But with VB this is not possible, because the Log method is a Sub, and there开发者_Python百科by does not produce a value.

I don't want to change the method to be a function.

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 Parameter not in scope


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


In 2010 if its a Sub and not a Function just replace Function with Sub.

logger.Verify(Sub(x) x.Log, Times.AtLeastOnce)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号