考虑这段代码 https://github.com/aspnet/Mvc/blob/master/src/Microsoft.AspNet.Mvc.Core/Builder/MvcApplicationBuilderExtensions.cs#L57来自 ASP.NET MVC 的源代码:
public static IApplicationBuilder UseMvc(
[NotNull] this IApplicationBuilder app,
[NotNull] Action<IRouteBuilder> configureRoutes) {...}
根据this https://stackoverflow.com/a/32528271/1646540答,注释的参数不能为空。那为什么我可以将 null 传递给该方法呢?也就是说,在下面的情况下,为什么编译器没有给我任何错误?
app.UseMvc(null);
唯一可能导致编译器生成错误的属性是ObsoleteAttribute https://msdn.microsoft.com/en-us/library/system.obsoleteattribute(v=vs.110).aspx。这是因为该属性的行为被硬编码到编译器中。
属性如NotNull
属性通常用于让工具(如 ReSharper)在编写代码时生成警告或错误。请阅读有关此特定属性的信息here https://www.jetbrains.com/help/resharper/Reference__Code_Annotation_Attributes.html#NotNullAttribute.
您还可以使用 PostSharp 等工具发出额外的构建时错误 http://www.postsharp.net/blog/post/Validating-parameters-field-and-properties-in-PostSharp-3.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)