当前位置: 首页 > news >正文

CS1988|C#无法在异步方法中使用ref,in,out类型的参数的问题

CS1988|C#无法在异步方法中使用ref,in,out类型的参数

🌀|场景:

请添加图片描述

BlazorServer的场景中推荐使用异步方法,使用ref,out,in为参数前缀则报错CS1988

原因如下:

ref parameters are not supported in async methods because the method may not have completed when control returns to the calling code. Any changes to the referenced variables will not be visible to the calling code, resulting in a CS1988 error.

async 方法不支持 ref 参数,因为当控件返回到调用代码时,该方法可能尚未完成。 对引用变量的任何更改都对调用代码不可见,从而导致 CS1988 错误。

⛵️|替代方法:

将需要用到的参数作为Task的返回,即

请添加图片描述

💥|延申:引用变量的可见性

1.当不使用基础类型为参数,使用引用类型时:

现有Blazor页面如下

@page "/login"@using KatexTest2.Models
@using KatexTest2.Utils
@inject MyAuthProvider provider<h3>LoginPage</h3><AuthorizeView><NotAuthorized>@if(Isfailed){<span>用户名或密码错误</span>}@if (test.Number==114.514M){<span>压力吗室内</span>}<EditForm id = "LP" Model="loginModel" Context="Login"><div><label> Username: <InputText @bind-Value="loginModel.Username"></InputText></label></div><div><label> Password:<InputText type="password" @bind-Value="loginModel.Password"></InputText></label></div><div><button @onclick="TryLogin">Submit</button></div></EditForm>    </NotAuthorized><Authorized><button type="button" class="btn btn-primary" @onclick="TryLogout" ></button></Authorized>    <Authorizing><span>翼沿丁真</span></Authorizing>
</AuthorizeView>@code {public class RefTest{public string Context { get; set; } = "DefaultContext";public decimal Number { get; set; } = 11.54M;}[Parameter]public Boolean Isfailed { get; set; } = false;public RefTest test { get; set; } = new();private LoginFormModel loginModel { get; set; } = new();private async Task TryLogin(){Isfailed  =  await provider.LoginAsync(loginModel,test);}private async Task TryLogout(){await provider.LogoutAsync();}
}

修改LoginAsync方法如下:

 public async Task<Boolean> LoginAsync(LoginFormModel loginFormModel,RefTest test)
{var (userInDatabase, isSuccess) = LookUpUser(loginFormModel.Username, loginFormModel.Password);var principal = new ClaimsPrincipal();if (isSuccess){var identity = CreateIdentityFromUser(userInDatabase);principal = new ClaimsPrincipal(identity);await _protectedLocalStorage.SetAsync("identity", JsonConvert.SerializeObject(userInDatabase));}else{test.Number = 114.514M;}NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(principal)));await jsRuntime.InvokeVoidAsync("alert", new object[1] { "WTF" });return !isSuccess;
}

运行测试:

请添加图片描述

从图中可以看出:

在方法的运行时间内,razor page并不能获取对象信息改变的值(运行完成后可以),如果是一些运行时间长的方法,则可能造成一些响应不及时的效果。例如拿掉权限后依旧可以访问一些东西

🔚|结论:

如果要在async方法中获取一些返回值,请直接加在的Task的返回中。

应避免使用引用类型为参数来充当返回值。

http://www.lryc.cn/news/135158.html

相关文章:

  • ubuntu开机失败——ACPI Error
  • 搭建开发环境-操作系统篇(一键搭建开发环境)
  • 人工智能AI绘画接入使用文档
  • 如何使用PyQt进行文件操作
  • 阿里云CDN加速器基本概念与购买开通
  • 2023河南萌新联赛第(六)场:河南理工大学-F 爱睡大觉的小C
  • [C++ 网络协议编程] 域名及网络地址
  • Java【HTTP】什么是 Cookie 和 Session? 如何理解这两种机制的区别和作用?
  • 使用U盘重装Windows10系统详细步骤及配图【官方纯净版】
  • 数据结构之——(手撕)顺序表
  • 冠达管理:非银金融是什么?
  • go 结构体
  • C++学习笔记---- 引用
  • 2023国赛数学建模思路 - 案例:感知机原理剖析及实现
  • Cesium加载Supermap的wmts服务
  • C/C++:C/C++在大数据时代的应用,以及C/C++程序员未来的发展路线
  • linux RabbitMQ-3.8.5 安装
  • 单链表Single-LinkList
  • AI嵌入式全景:各厂商、系列和开发工具的综合概览
  • mysql Left Join on条件 where条件的用法区别
  • Redis中的淘汰策略
  • MyBatis进阶:掌握MyBatis动态SQL与模糊查询、结果映射,让你在面试中脱颖而出!!
  • C++ 写入txt文件内容并追加内容
  • Leetcode---359周赛
  • Keras三种主流模型构建方式:序列模型、函数模型、子类模型开发实践,以真实烟雾识别场景数据为例
  • objective-v 获取iPhone系统当前时间字符串适配12小时制和24小时制
  • 并查集及其简单应用
  • 基于web的服装商城系统java网上购物商店jsp源代码mysql
  • .NET Core发布到IIS
  • Spring的基本概念