c# - NotNull 属性

标签 c# asp.net-core

我正在查看 asp.net vnext engineering guideline并注意到他们建议使用 NotNull 属性,而不是显式检查输入参数是否为 null 并抛出 ArgumentNullException。令我困惑的是,基于这个指南,声明这个属性就足够了,检查代码将在编译时生成到方法体中。我已尝试在我的测试项目中执行此操作,但奇迹并没有发生,即它抛出了异常 System.NullReferenceException 而不是 System.ArgumentNullException。这应该如何工作?他们会使用一些 AOP 库在编译时注入(inject)检查代码吗?

最佳答案

NotNullAttribute 消失了。这是replaced with conditionally throwing ArgumentNullException随后 removed由 ASP.NET 团队。截至 2016 年 1 月 12 日,没有计划将其恢复。 (当时,我在 ASP.NET 团队工作。)


该属性将通过使用 Roslyn 的预编译步骤由执行实际检查的代码替换。

但是,截至 2015 年 6 月 17 日,该功能尚未准备就绪。它将在以后的版本中提供。到目前为止,它只是an empty internal attributeshould be implemented in each project again :

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
internal sealed class NotNullAttribute : Attribute
{
}

<罢工>

关于c# - NotNull 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30896853/

相关文章:

c# - MessageBox.Show 问题

c# - 具有自定义名称的 MVC APIController

asp.net-core - 使用 EF Core Migration 更新现有列中的数据

asp.net-core - 如何阻止 AWS API Gateway 对二进制响应进行 Base64 编码?

C# - 从配置文件中检索文件路径 - @ 并不神奇

c# - 添加到 System.Web.Caching.Cache 的项目的默认行为是什么?

c# - 将字符串列表作为参数传递给控制台应用程序

c# - 保存到数据库需要太长时间

c# - 找不到适合类型 'RestDataService' 的构造函数

asp.net-core - Saml 无 Cookie 保留状态 ASP.NET CORE