欧克:操纵传递的参数不起作用

发布于 2025-02-13 06:54:36 字数 517 浏览 2 评论 0原文

我有这样的东西:

MyMock
.Setup(m => m.MyMethodAsync(It.IsAny<Stream>()))
.Returns((Stream outStream) =>
{
   outStream = new MemoryStream(Encoding.UTF8.GetBytes("this is the content"));
   return Task.CompletedTask;
});

可以编译和执行。我可以看到运行代码的调试器。但是该方法的来电者无法获得流的值。我想念什么?

EDIT1:

更改返回到回调的行为相同

.Callback((MT365ReceiveResult result, Stream outStream) =>
{
    outStream = new MemoryStream(Encoding.UTF8.GetBytes("this is the content"));
});

I have something like this:

MyMock
.Setup(m => m.MyMethodAsync(It.IsAny<Stream>()))
.Returns((Stream outStream) =>
{
   outStream = new MemoryStream(Encoding.UTF8.GetBytes("this is the content"));
   return Task.CompletedTask;
});

this can be compiled and executed. I can see the debugger running the code. But the caller of this method do not get the value of the Stream. What do i miss?

Edit1:

Change Returns to Callback has the same behavior

.Callback((MT365ReceiveResult result, Stream outStream) =>
{
    outStream = new MemoryStream(Encoding.UTF8.GetBytes("this is the content"));
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2025-02-20 06:54:36

示例的代码不使用该方法呼叫者提供的,而是将新的MemoryStream分配给参数。由于参数未用ref或 out 标记,因此此重新分配仅在方法中有效,但在外部不有效。

我想,您想为呼叫者提供的写一个值。您可以使用以下代码执行此操作:

MyMock
  .Setup(m => m.MyMethodAsync(It.IsAny<Stream>()))
  .Returns((Stream outStream) =>
  {
    var bytes = Encoding.UTF8.GetBytes("this is the content");
    outStream.Write(bytes, 0, bytes.Length);
    return Task.CompletedTask;
  });

The code of the sample does not use the Stream that is provided by the caller of the method, but assigns a new MemoryStream to the parameter. As the parameter is not marked with ref or out, this reassignment is only valid in the method, but not outside.

I suppose, you want to write a value to the Stream that is provided by the caller. You can do this with the following code:

MyMock
  .Setup(m => m.MyMethodAsync(It.IsAny<Stream>()))
  .Returns((Stream outStream) =>
  {
    var bytes = Encoding.UTF8.GetBytes("this is the content");
    outStream.Write(bytes, 0, bytes.Length);
    return Task.CompletedTask;
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文