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

c语言 做网站响应式网页设计技巧

c语言 做网站,响应式网页设计技巧,施工企业资质等级承包范围,广州学生做网站C# 10 新特性 —— CallerArgumentExpressionIntroC# 10 支持使用 CallerArgumentExpression 来自动地获取调用方的信息#xff0c;这可以简化我们现在的一些代码#xff0c;让代码更加简洁#xff0c;一起看下面的示例吧Caller InfoC# 在 5.0 的时候开始支持 Caller Info 自… C# 10 新特性 —— CallerArgumentExpressionIntroC# 10 支持使用 CallerArgumentExpression 来自动地获取调用方的信息这可以简化我们现在的一些代码让代码更加简洁一起看下面的示例吧Caller InfoC# 在 5.0 的时候开始支持 Caller Info 自动获取调用方的一些信息C# 5 开始支持的 Caller Info Attribute 有三个,[CallerMemberName] - 调用方成员名称方法名或者属性名.[CallerFilePath] - 调用方源代码所在文件路径[CallerLineNumber] - 调用方所在源代码的行数信息在方法参数中添加一个 Attribute 来获取调用方信息使用示例如下public static void MainTest() {// 我是谁我在哪儿DumpCallerInfo(); }private static void DumpCallerInfo([CallerFilePath] string? callerFilePath  null,[CallerLineNumber] int? callerLineNumber  null,[CallerMemberName] string? callerMemberName  null ) {Console.WriteLine(Caller info:);Console.WriteLine($CallerFilePath: {callerFilePath} CallerLineNumber: {callerLineNumber} CallerMemberName: {callerMemberName} ); }针对 CallerLineNumber 的类型是 int所以参数类型需要能够直接接收 int如上面的 [CallerLineNumber] int? callerLineNumber null 也可以改成 [CallerLineNumber] int callerLineNumber -1 或者 [CallerLineNumber] long callerLineNumber -1但是不能改成 [CallerLineNumber] string callerLineNumber null 或者 [CallerLineNumber] short callerLineNumber -1string 类型不兼容short 不能隐式转换上面代码输出结果类似下面Caller info: CallerFilePath: C:\projects\sources\SamplesInPractice\CSharp10Sample\CallerInfo.cs CallerLineNumber: 8 CallerMemberName: MainTestCallerArgumentExpressionCallerArgumentExpression 也是属于一种 Caller Info下面这里是利用 CallerArgumentExpression 实现的几个验证方法如果参数不合法就抛出一个异常通过 CallerArgumenExpression 来自动的获取调用方的参数表达式public static class Verify {public static void Argument(bool condition, string message, [CallerArgumentExpression(condition)] string? conditionExpression  null){if (!condition) throw new ArgumentException(message: message, paramName: conditionExpression);}public static void NotNullOrEmpty(string argument, [CallerArgumentExpression(argument)] string? argumentExpression  null){if (string.IsNullOrEmpty(argument)){throw new ArgumentException(Can not be null or empty, argumentExpression);}}public static void InRange(int argument, int low, int high,[CallerArgumentExpression(argument)] string? argumentExpression  null,[CallerArgumentExpression(low)] string? lowExpression  null,[CallerArgumentExpression(high)] string? highExpression  null){if (argument  low){throw new ArgumentOutOfRangeException(paramName: argumentExpression,message: ${argumentExpression} ({argument}) cannot be less than {lowExpression} ({low}).);}if (argument  high){throw new ArgumentOutOfRangeException(paramName: argumentExpression,message: ${argumentExpression} ({argument}) cannot be greater than {highExpression} ({high}).);}}public static void NotNullT(T? argument, [CallerArgumentExpression(argument)] string? argumentExpression  null)where T : class{ArgumentNullException.ThrowIfNull(argument, argumentExpression);} }来看一个使用调用示例var name  string.Empty; InvokeHelper.TryInvoke(()  Verify.NotNullOrEmpty(name));上面的 InvokeHelper.TryInvoke 是封装的一个方法如果有异常会记录一个日志上面代码执行结果如下可以看到我们的名称也是被记录了下来 Parameter 名字就是我们传入的变量名不需要我们再手动的额外加一个 nameof(name) 了再来看一个调用示例调用代码如下var num  10; InvokeHelper.TryInvoke(()  Verify.InRange(num, 2, 5)); InvokeHelper.TryInvoke(()  Verify.InRange(num, 10  2, 10  5));输出结果如下如果没有变量名或者属性名等就会直接用传入进来的 value 字面量如果传入进来的是一个表达式那么记录下来的就是表达式本身比如上面输出的 5/10 2而 num 是传入的一个变量就会获取到变量的名字是不是很神奇很多验证的地方就可以简化很多了SampleCallerArgumentExpression 有一个很典型的一个实际应用就是 .NET 6 里新增的一个 APIArgumentNullException.ThrowIfNull() 方法这个方法的实现如下/// summaryThrows an see crefArgumentNullException/ if paramref nameargument/ is null./summary /// param nameargumentThe reference type argument to validate as non-null./param /// param nameparamNameThe name of the parameter with which paramref nameargument/ corresponds./param public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(argument)] string? paramName  null) {if (argument is null){Throw(paramName);} }[DoesNotReturn] private static void Throw(string? paramName) throw new ArgumentNullException(paramName);源码可以从 Github 上看 https://github.com/dotnet/runtime/blob/v6.0.0/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs我们实际调用的时候就可以不传参数名会自动的获取参数名示例如下object? xiaoMing  null; InvokeHelper.TryInvoke(()  ArgumentNullException.ThrowIfNull(xiaoMing));输出结果如下从上面的结果我们可以看到参数名已经自动的解析出来了More升级 .NET 6 的小伙伴快用这个改造你的代码吧然后就是很多 null 检查也可以使用新的 ArgumentNullException.ThrowIfNull 去简化代码吧~~想使用上述代码测试可以从 Github 获取 https://github.com/WeihanLi/SamplesInPractice/blob/master/CSharp10Sample/CallerInfo.csReferenceshttps://www.codeproject.com/Tips/606379/Caller-Info-Attributes-in-Csharp-5-0https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/caller-argument-expression?WT.mc_idDT-MVP-5004222https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/caller-information?WT.mc_idDT-MVP-5004222https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10?WT.mc_idDT-MVP-5004222#callerargumentexpression-attribute-diagnosticshttps://github.com/dotnet/runtime/blob/v6.0.0/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cshttps://github.com/WeihanLi/SamplesInPractice/blob/master/CSharp10Sample/CallerInfo.cs
http://www.pierceye.com/news/716303/

相关文章:

  • 电力建设期刊网站投稿域名提供商
  • 广东网站备案需要多久oa信息化管理系统平台
  • 哪个网站可以做担保交易小程序排行榜
  • 网站用html做的怎么弄后台中铁十六局个人门户网
  • 一个网站怎么做流量统计佛山市seo广告优化工具
  • 机关网站建设需求文档国家住建部官网
  • 一条龙网站建设哪家好六安招聘网官网
  • 网站建设 中企动力阀门和建设银行类似的网站
  • 所有做运动的网站姜堰网网站
  • 广西汽车网网站建设影楼微网站建设方案
  • 企业展厅设计比较好的公司北京优化服务
  • 网站的icp 备案信息wordpress爆破字典
  • 福建厦门网站建设公司网站代码素材建设
  • 广州网络公司建站e语言可以做网站吗
  • 不想用原来的网站模板了就用小偷工具采集了一个可是怎么替换seo顾问张智伟
  • 效果好的徐州网站开发建设网站怎么学
  • 上海网站设计要多少钱建设银行个人网站打不开
  • 哪个网站做欧洲旅行比较好东营网站制作
  • 做pc端网站效果wordpress js 添加图片
  • 给装修公司做网站商标设计大全
  • 深圳做网站公司有哪些地方国际形势最新消息
  • 企业网站建设管理平台石家庄平山网站推广优化
  • 免费asp网站模板带后台网站建设需求调研通知
  • 浙江二建建设集团有限公司网站微信哪里可以做视频网站
  • wordpress阿里百秀5.2广州网站排名专业乐云seo
  • 网站建设 上海网站福州最好的网站建设公司
  • 兴力网站建设企业宣传网站在哪里做
  • 网站了建设pc官网 和手机网站
  • wordpress导航网站模板下载wordpress 关闭搜索引擎
  • 网站架构的优化wordpress企业主题免费下载