使用 ref 参数模拟方法的语法
我遇到的问题是无法模拟具有 ref 参数的方法。 我想要模拟的方法的签名如下:
class ContractRepository
...
public long GetValueAndIncrement(ref Counter counter)
{
...
}
我尝试像这样模拟它:
Random myRandomizer = new Random();
var contractRepo = new SIContractRepository();
contractRepo.GetValueAndIncrementCounterRef = ((internalCounter) => Int64.Parse(myRandomizer.Next().ToString()));
但是编译器告诉我我缺少“ref”关键字,但是当我这样尝试时,
Random myRandomizer = new Random();
var contractRepo = new SIContractRepository();
contractRepo.GetValueAndIncrementCounterRef = ((ref internalCounter) => Int64.Parse(myRandomizer.Next().ToString()));
我收到一个错误,即 ref是一个无效的表达式
不幸的是,谷歌在这里没有帮助。 :( 有什么想法吗?
I have the problem that I can't mock a method that has a ref argument.
The signature of the method I want to mock away is as follows:
class ContractRepository
...
public long GetValueAndIncrement(ref Counter counter)
{
...
}
I tried to mock it like this:
Random myRandomizer = new Random();
var contractRepo = new SIContractRepository();
contractRepo.GetValueAndIncrementCounterRef = ((internalCounter) => Int64.Parse(myRandomizer.Next().ToString()));
But the compiler tells me that I am missing the "ref" keyword, but when I try it like this
Random myRandomizer = new Random();
var contractRepo = new SIContractRepository();
contractRepo.GetValueAndIncrementCounterRef = ((ref internalCounter) => Int64.Parse(myRandomizer.Next().ToString()));
I get an error that ref is an invalid expression
Unfortunately, google doesn't help here. :(
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这种情况下,您根本无法使用匿名方法,因为它们既不支持 ref 也不支持 out 参数。您需要创建一个“真实”的方法。
You simply can't use anonymous methods in this case, because they support neither ref nor out parameters. You need to create a "real" method.
您可以通过ref关键字使用匿名方法,只需在匿名方法中显式指定类型即可:
You can use anonymous methods with the ref keyword, just explicitly specify the type in the anonymous method:
请记住,当前版本的 Moles 仅支持 ref 和 out 参数作为方法的 LAST 参数。
http://research.microsoft.com/en-us/projects/pex /molesmanual.pdf
限制
目前 Moles 的实现有几个限制。这些限制并不
该方法固有的,可能会在 Moles 的未来版本中得到解决:
Moles 框架仅支持有限数量的方法签名——最多
10 个参数,其中最后一个参数可以是 out 或 ref 参数。
不支持带有指针的方法签名。
密封类或静态方法不能被存根,因为存根类型依赖于
虚拟方法调度。对于这种情况,请使用“摩尔”中描述的摩尔类型
类型”见本文档后面的内容
Remember that the current version of Moles only supports ref and out arguments as the LAST argument of a method.
http://research.microsoft.com/en-us/projects/pex/molesmanual.pdf
Limitations
The current implementation of Moles has several limitations. These limitations are not
inherent to the approach and might be resolved in future releases of Moles:
The Moles framework supports only a limited number of method signature—up to
10 arguments, where the last argument can be an out or ref argument.
Method signatures with pointers are not supported.
Sealed classes or static methods cannot be stubbed because stub types rely on
virtual method dispatch. For such cases, use mole types as described in “Mole
Types” later in this document
我不确定这是否是涂抹痣的正确方法,但我做到了。它有效。
当这部分在测试运行期间执行时,新的p2可用。以下是我的场景。
它具有新的价值,但可能有更好的方法。
I am not sure whether this is a correct way to apply moles but I did it. And it works.
When this part executed during test run, new p2 is available. Below was my scenario.
It works with new value but there could be a better way.