我正在查看 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 attribute那should 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/