NotNull attribute

C#asp.net Core

C# Problem Overview


I'm looking at asp.net vnext engineering guideline and have noticed that they recommend to use NotNull attribute instead of explicit checking of input argument for null and throwing ArgumentNullException. The thing that confused me is that base on this guideline it is just enough to declare this attribute and the checking code will be generated at compile time into the method body. I've tried to do this in my test project however the magic didn't happen i.e. it threw the exception System.NullReferenceException instead of System.ArgumentNullException. How this is supposed to work? Are they going to use some AOP library to inject the checking code at compile time?

C# Solutions


Solution 1 - C#

The NotNullAttribute is gone. It was replaced with conditionally throwing ArgumentNullException and subsequently removed by the ASP.NET team. As of Jan 12, 2016 there's no plan to bring it back. (At that time, I was working on the ASP.NET team.)


The attribute will be replaced through a pre-compilation step, using Roslyn, by code that does the actual check.

However, the feature is not yet ready as of Jun 17, 2015. It will come in a later version. So far, it is just an empty internal attribute that should be implemented in each project again:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexey AndrushkevichView Question on Stackoverflow
Solution 1 - C#Victor HurdugaciView Answer on Stackoverflow