使用[NOTNULL对于一个方法的参数 [英] Using [NotNull] for a method's parameters

查看:1260
本文介绍了使用[NOTNULL对于一个方法的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这从ASP.NET MVC的源代码代码:

public static IApplicationBuilder UseMvc(
            [NotNull] this IApplicationBuilder app,
            [NotNull] Action<IRouteBuilder> configureRoutes)
        {

基于这个回答,注释的参数不能为null。 ?但为什么我可以通过空该方法

based on this answer, the annotated parameters must not be null. but why I can pass null for that method?

app.UseMvc(null);

在这种情况下,为什么编译器不给我任何错误?

In this case, Why the compiler doesn't give me any error?

推荐答案

这可能会导致编译器产生一个错误的唯一属性是的 ObsoleteAttribute 。正是因为这个属性的行为是硬编码到编译器。

The only attribute that can cause the compiler to generate an error is the ObsoleteAttribute. It is because this attribute's behavior is hard-coded into the compiler.

属性,如 NOTNULL 属性一般指为工具(如ReSharper的)生成警告或错误的一边写代码。请阅读有关这种特殊属性这里

Attributes like the NotNull attribute are generally meant for tools (like ReSharper) to generate warnings or errors while writing code. Please read about this particular attribute here.

您也可以使用工具,如PostSharp到的增发编译时错误

You can also use tools like PostSharp to issue additional build-time errors.

这篇关于使用[NOTNULL对于一个方法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆