AssignAll 1.7.1-beta02

This is a prerelease version of AssignAll.
There is a newer version of this package available.
See the version list below for details.
dotnet add package AssignAll --version 1.7.1-beta02                
NuGet\Install-Package AssignAll -Version 1.7.1-beta02                
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="AssignAll" Version="1.7.1-beta02" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AssignAll --version 1.7.1-beta02                
#r "nuget: AssignAll, 1.7.1-beta02"                
#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 AssignAll as a Cake Addin
#addin nuget:?package=AssignAll&version=1.7.1-beta02&prerelease

// Install AssignAll as a Cake Tool
#tool nuget:?package=AssignAll&version=1.7.1-beta02&prerelease                

Build status

roslyn-analyzers

Collection of useful Roslyn analyzers and code fixes.

AssignAll

Diagnostic error if there are unassigned members in an object initializer and a code fix to add the missing ones.

This is particularly useful when mapping between types, such as DTO types and database entity types. It ensures you don't forget to update the mapping when a new property is added or refactored.

I previously used AutoMapper to help with that, but now I can finally go back to good old object initializers that in my experience are easier to set up and as performant as can be.

Installation

  1. Install nuget AssignAll
  2. Add comment // AssignAll enable somewhere above the object initializers you want to analyze

Sample

private static void UnassignedMembersGiveBuildError()
{
    // AssignAll enable
    var foo = new Foo
    {
        // UnassignedProp and UnassignedField not assigned, diagnostic error lists both
        AssignedProp = true
    };
}

private class Foo
{
    public bool AssignedProp { get; set; }
    public int UnassignedProp { get; set; }
    public int UnassignedField;
}

Red squigglies on unassigned members in object initializer

Error list describes what properties or fields are not assigned.

Enable/disable by comments

Analysis must be explicitly enabled by a special comment, and can be disabled and re-enabled to only apply analysis to certain blocks of code. These comments can occur anywhere in the file and affects all the code below it, or until the next special comment.

// AssignAll enable
Foo foo = new Foo
{
    // PropInt not assigned, diagnostic error

    // AssignAll disable
    Bar = new Bar
    {
        // PropInt not assigned, but analyzer is disabled, no diagnostic error

        // AssignAll enable
        Baz = new Baz
        {
            // PropInt not assigned, analysis re-enabled, diagnostic error
        }
    }
};

Ignore properties and fields

Simply comment out the member assignments you want to ignore. This is particularly convenient when using the codefix to first generate all member assignments, then commenting out the ones you want to skip.

// AssignAll enable
var foo = new Foo
{
    // Ignore these assignments by commenting them out, it is whitespace tolerant
    // PropIgnored1 = ,
    //PropIgnored2=2,

    // PropUnassigned is unassigned and not commented out, diagnostic error
    PropAssigned = 1
};

private class Foo
{
    public int PropIgnored1 { get; set; }
    public int PropIgnored2 { get; set; }
    public int PropAssigned { get; set; }
    public int PropUnassigned { get; set; }
}

Code fix: Assign all members

Quickly populate all missing property assignments with an empty value so it does not compile until you assign all the values. This saves a lot of typing and intellisense browsing.

Apply code fix 'Assign all members'

Future improvements

  • Similar diagnostic for constructors (Constructor_AssignAll)
  • Auto-fill mapping members with identical or similar names
  • Code fix to ignore remaining missing members
  • Attributes to enable analysis for certain types
  • Attributes to ignore properties/fields
  • Configuration to enable/disable by default
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.9.0 42,616 8/10/2023
1.8.0 12,696 3/18/2023
1.7.2 991 3/8/2023
1.7.1-beta02 3,112 10/6/2022
1.7.1-beta01 217 10/6/2022
1.6.1 6,202 10/5/2022
1.6.0 2,906 10/2/2022
1.5.2 462 10/1/2022
1.5.1 457 10/1/2022
1.5.0 137,360 12/22/2019
1.4.1 569 12/22/2019
1.4.0 549 12/22/2019
1.3.0 559 12/22/2019
1.2.0.30 534 12/22/2019
1.1.0 568 12/22/2019
1.0.6345.28705 4,442 5/16/2017