RuntimeNullables.Fody
2.0.0
dotnet add package RuntimeNullables.Fody --version 2.0.0
NuGet\Install-Package RuntimeNullables.Fody -Version 2.0.0
<PackageReference Include="RuntimeNullables.Fody" Version="2.0.0" />
paket add RuntimeNullables.Fody --version 2.0.0
#r "nuget: RuntimeNullables.Fody, 2.0.0"
// Install RuntimeNullables.Fody as a Cake Addin #addin nuget:?package=RuntimeNullables.Fody&version=2.0.0 // Install RuntimeNullables.Fody as a Cake Tool #tool nuget:?package=RuntimeNullables.Fody&version=2.0.0
Runtime Nullables
Runtime Nullables automatically adds null checks to method/property entry and exit points based on the standard nullable annotations and attributes available in C# 8+. It is capable of checking input parameters as well as outputs (i.e. return values and out
/ref
parameters) and supports comprehensive checks on the full range of special method types including asynchronous Task<T>
methods, Task<T>
methods that synchronously return completed tasks (i.e. using Task.FromResult<T>
), IEnumerable<T>
/IEnumerator<T>
iterators as well as asynchronous IAsyncEnumerable<T>
/IAsyncEnumerator<T>
iterators. Custom throw helpers can be defined to fully customize the exceptions thrown to your liking.
About Singulink
We are a small team of engineers and designers dedicated to building beautiful, functional and well-engineered software solutions. We offer very competitive rates as well as fixed-price contracts and welcome inquiries to discuss any custom development / project support needs you may have.
Visit https://github.com/Singulink to see our full list of publicly available libraries and other open-source projects.
Installation
The package is available on NuGet - simply install the RuntimeNullables.Fody
package into your project and null checks will be automatically injected when your project builds! It is also a good idea to add the latest Fody
package directly to your project to ensure you are using an up-to-date version to avoid issues.
Runtime Nullables v.s. NullGuard.Fody
After adding nullable annotation support to NullGuard on top of its legacy functionality, it was apparent that it become rather unweildy. This made expanding features and optimizing behavior for nullable annotations difficult in addition to inheriting an overly complex attribute model. Runtime Nullables was written from the ground up with only nullable annotations in mind to address these issues. Some notable improvements include:
- More efficient weaving algorithm for faster build times.
- Does not force
.initlocals
on methods (better performance, especially withstackalloc
). - Supports checking
ValueTask<T>
results, synchronously returned completeTask<T>
results,IEnumerable<T>
/IEnumerator<T>
iterator values and asynchronousIAsyncEnumerable<T>
/IAsyncEnumerator<T>
iterator values. - Uses throw helpers instead of throwing directly (better performance / smaller IL code) and lets you define your own custom throw helpers.
- Much simpler attribute model that uses a single
[NullChecks(bool)]
attribute to control null check injection. - Designed specifically for NRTs so it is much easier to add advanced functionality such as full validation of conditional attributes like
[MaybeNullWhen]
(planned for a future release). - Outputs warnings for conflicting annotations that cause null checks to be skipped, i.e. if
[AllowNull, DisallowNull]
is applied to a parameter. - Uses
NullReferenceException
instead ofInvalidOperationException
when an output check fails since the latter is often thrown/caught in normal circumstances which can cause null contract violations to go unnoticed in unit tests or exception handling code. - Numerous bug fixes and reliability improvements.
Configuration
By default, null checks are added based on the build configuration as follows:
Release
: input parameter values on publicly exposed methods/properties onlyDebug
: input and output (ref
/out
) parameter values and return values on all methods/properties
The behavior can be configured in one of two ways: inside your .csproj
file or using a FodyWeavers.xml
configuration file in the root of your project. There are two configuration settings:
CheckOutputs
: If set totrue
then outputs (i.e. return values, ref/out parameters) will be null checked, otherwise these checks are omitted.CheckNonPublic
: If set totrue
then all methods/properties are checked, otherwise only those that are potentially publicly exposed are checked.
If configuring via the .csproj
file then you have to add a <WeaverConfiguration>
property like so:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<WeaverConfiguration>
<Weavers>
<RuntimeNullables CheckNonPublic="true" CheckOutputs="true" />
</Weavers>
</WeaverConfiguration>
</PropertyGroup>
</Project>
If you would like to configure settings with a FodyWeavers.xml
file in the project then simply build the project after adding the RuntimeNullables.Fody
package and it will automatically generate an xml configuration file for you which you can then edit. Alternatively, you can add the file manually with the following contents:
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<RuntimeNullables CheckNonPublic="true" CheckOutputs="true" />
</Weavers>
If you omit the CheckNonPublic
or CheckOutputs
setting then it will fallback to the default behavior based on the build configuration as explained above.
Fine-Tuning Behavior
Injection of null checks can be fine-tuned in your code using the [NullChecks(bool)]
attribute which can be applied at the assembly, class, method/property or return value level. For example, if you apply [NullChecks(false)]
to a class it will disable null checks on every member in that class unless it has [NullChecks(true)]
applied to it. It is important to note that if the CheckOutputs
or CheckNonPublic
setting is false
then that will override any [NullChecks(true)]
attribute applied to an output or non-public member. In other words, if the setting is set to false
then those particular checks are never emitted, regardless of the presence of a [NullChecks(true)]
attribute.
Additionally, custom throw helpers can be defined to customize the exceptions that are thrown. In order to do that, you simply add an internal ThrowHelpers
class into your project in the RuntimeNullables
namespace and match the signature of the throw helper you want to override and it will be used instead of the defaults. The default throw helpers are as follows:
namespace RuntimeNullables
{
internal static class ThrowHelpers
{
internal static void ThrowArgumentNull(string paramName)
{
throw new ArgumentNullException(paramName);
}
internal static void ThrowOutputNull(string message)
{
throw new NullReferenceException(message);
}
internal static Exception GetAsyncResultNullException(string message)
{
return new NullReferenceException(message);
}
}
}
If an output check or async result check fails then the message
parameter contains a message specifying exactly what caused the check to fail, i.e. "Enumerator result nullability contract was broken."
Examples
using RuntimeNullables;
// Return value is not null checked since it is nullable,
// but method throws ArgumentNullException if messageService is null
public string? GetMessage(MessageService messageService);
// The 'value' parameter is null checked on method entry as well as when the method exits since it is a ref
public void UpdateMessage(ref string value);
// Now the 'value' parameter is only null checked on method entry due to the [MaybeNull] annotation
public void UpdateMessage([MaybeNull] ref string value);
// Each item returned by the enumeration is null checked
public IEnumerable<string> GetMessages()
{
string? nullValue = null;
yield return "some value";
yield return "some other value";
yield return nullValue!; // NullReferenceException is thrown
}
public Task<T> GetValueAsync<T>()
{
return Task.FromResult<T>(default!); // NullReferenceException if T is a reference type
}
[return: NullChecks(false)]
public Task<T> GetValueAsync<T>()
{
return Task.FromResult<T>(default!); // This is okay now since return value null checks are disabled
}
// Result of the task is null checked
public async Task<string> GetMessageAsync()
{
return await MessageService.GetMessageAsync();
}
// All results are null checked
public async IAsyncEnumerable<string> GetValuesAsync()
{
await Task.Delay(100);
yield return "Some value";
await Task.Delay(100);
yield return null!; // NullReferenceException is thrown
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Fody (>= 6.8.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on RuntimeNullables.Fody:
Repository | Stars |
---|---|
openmod/openmod
OpenMod .NET Plugin Framework
|
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 195 | 10/25/2024 |
1.0.6 | 23,517 | 9/21/2023 |
1.0.5 | 188,094 | 5/19/2023 |
1.0.4 | 20,915 | 12/31/2022 |
1.0.2 | 96,619 | 7/10/2021 |
1.0.1 | 2,687 | 5/6/2021 |
1.0.0 | 620 | 5/6/2021 |
0.9.8 | 563 | 3/11/2021 |
0.9.7 | 21,345 | 2/18/2021 |
0.9.6 | 5,592 | 1/31/2021 |
0.9.5 | 379 | 1/30/2021 |
0.9.4 | 341 | 1/30/2021 |
0.9.3 | 889 | 9/1/2020 |
0.9.2 | 965 | 7/28/2020 |
0.9.1 | 440 | 7/25/2020 |