NullGuard
An experiment in guarding against null arguments
Install / Use
/learn @haacked/NullGuardREADME
NullGuard
The null reference has been called "my billion dollar mistake" by its inventor, Tony Hoare. Guarding against null arguments and return values ends up resulting in a lot of boilerplate code.
This library attempts to remove the repetitive work of all that argument null checking by rewriting IL to check all nullable arguments. For more details, check out the blog post announcing this library.
Install this library.
Install-Package NullGuard.PostSharp
You can apply the EnsureNonNullAspect attribute to individual classes, methods, or an entire assembly. You'll also
need to install the PostSharp Visual Studio Extension. This library
should work fine with the free community edition.
Usage Examples
using NullGuard;
[assembly: EnsureNonNullAspect]
public class Sample
{
public void SomeMethod(string arg) {
// throws ArgumentNullException if arg is null.
}
public void AnotherMethod([AllowNull]string arg) {
// arg may be null here
}
public string MethodWithReturn() {
// Throws InvalidOperationException if return value is null.
}
// Null checking works for automatic properties too.
public string SomeProperty { get; set; }
[AllowNull] // can be applied to a whole property
public string NullProperty { get; set; }
public string NullProperty {
get;
[param: AllowNull] // Or just the setter.
set;
}
Note
For this specific aspect, the code generated by PostSharp Professional Edition 2.1 will be much cleaner and faster than the one generated by PostSharp Starter Edition 2.1 because of the optimizer included in the paid edition. The implementation with PostSharp 3 will be even more better.
Related Skills
node-connect
344.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
96.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
