Using [NotNull] for a method's parameters

后端 未结 2 1083
离开以前
离开以前 2021-02-03 20:01

Consider this code from ASP.NET MVC\'s source code:

public static IApplicationBuilder UseMvc(
            [NotNull] this IApplicationBuilder app,
            [No         


        
相关标签:
2条回答
  • 2021-02-03 20:27

    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.

    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.

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

    0 讨论(0)
  • 2021-02-03 20:35

    If you wish to move Null checks to be implemented by aspects and not need to be done by hand. The clear solution is to use Fody the open source build weaver. Specifically you want to leverage the NullGuard Fody

    PM> Install-Package NullGuard.Fody

    Should be everything required to get set up to use Fody with null guards. The documentation shows how you can have fine grain control if you desire.


    2019 Update

    C# 8 and .NET Core 3.0 permanently eliminate null reference exceptions.

    Tutorial: Migrate existing code with nullable reference types

    Tutorial: Express your design intent more clearly with nullable and non-nullable reference types

    0 讨论(0)
提交回复
热议问题