NotNull 属性

NotNull attribute

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

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


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

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

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