MustInitializeAnalyzer 1.0.0

Suggested Alternatives

SequelPay.DotNetPowerExtensions.Analyzers

Additional Details

The package is now separated in two SequelPay.DotNetPowerExtensions and SequelPay.DotNetPowerExtensions.Analyzers, for more details and features check out the GitHub repo

There is a newer version of this package available.
See the version list below for details.
dotnet add package MustInitializeAnalyzer --version 1.0.0                
NuGet\Install-Package MustInitializeAnalyzer -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MustInitializeAnalyzer" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MustInitializeAnalyzer --version 1.0.0                
#r "nuget: MustInitializeAnalyzer, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MustInitializeAnalyzer as a Cake Addin
#addin nuget:?package=MustInitializeAnalyzer&version=1.0.0

// Install MustInitializeAnalyzer as a Cake Tool
#tool nuget:?package=MustInitializeAnalyzer&version=1.0.0                

MustInitialize Analyzer

What is the purpose of MustInitialize Analyzer?

  • Allows you enforce that the given property or field has to be initialized when instantiated.
  • Removes the need to set a value when in a nullable context (in C# 8 and upwards) for such a property or field
Example Code
1.   public class TestClass
2.   {
3.       [MustInitialize] public string TestProperty { get; set; } 
4.   }
5.   var testObj = new TestClass();

Without MustInitialize the following error will be reported on line 3

warning CS8618: Non-nullable field 'TestProperty' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

However with MustInitialize it will report the following on line 5

warning MustInitialize: Property 'TestProperty' must be initialized

Componenets of MustInitialize Analyzer

  • A Roslyn analyzer which can be installed as a Nuget package
  • A Visual Studio extension

Installing as Nuget from local

Add in the same folder as the .sln file for your project a file named nuget.config with the following content (and replace Path/to/dll/folder with the actual path for the nupkg is stored)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <add key="MyLocalSharedSource" value="Path/to/nupkg/folder" />
    </packageSources>
</configuration>

Afterwards it will show up as a package source in the Nuget Package sources, so you can install it from there

See sample image from the Visaul Studio Tools -> Nuget Package Manager -> Manage Nuget Packages for Solution... page

image

Debugging

To run in Visual Studio

Just load up the project nad hit start.

When running the project sometimes the breakpoints are not being hit, you can use the suggestions described in this issue: - Turn off Use 64 bit process for code analysis - Add a Debugger.Launch() statement

To run in dnSpy

Here are the steps involved:

  1. Open VS Develoepr Command Prompt and run MS build on your project

  2. From the output get the entire csc command line

    • Since it won't fit on the command line save the entire string (besides the csc path) in a file named options.rsp
  3. Open dnSpy

    • Use the 64 Bit version if you are on a x64 machine and csc is in Program Files not in Program Files (x86), otherwise use the 32 Bit version
  4. From File -> Open load all MustInitializeAnalyzer.dll files referenced in the string of step 2

    • Set breakpoints in all loaded files (to ensure that it is breaking)
  5. Run Start in dnSpy and set the following parameters:

    • Executable: Should be set to the csc path of step 2
    • Arguments: Should be @"options.rsp" (the full path of the file of step 2)
    • Working Directory: Should be set the directory of your .csproj file to be debugged
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Summary of changes made in this release of the package.