AssignAll 1.6.1
See the version list below for details.
dotnet add package AssignAll --version 1.6.1
NuGet\Install-Package AssignAll -Version 1.6.1
<PackageReference Include="AssignAll" Version="1.6.1" />
paket add AssignAll --version 1.6.1
#r "nuget: AssignAll, 1.6.1"
// Install AssignAll as a Cake Addin #addin nuget:?package=AssignAll&version=1.6.1 // Install AssignAll as a Cake Tool #tool nuget:?package=AssignAll&version=1.6.1
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
- Sample
- Enable/disable by comments
- Ignore properties and fields
- Code fix: Assign all members
- Future improvements
Installation
- Install nuget AssignAll
- 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;
}
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.
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
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.Analyzers (>= 2.9.8)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 3.4.0)
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 | 28,666 | 8/10/2023 |
1.8.0 | 12,511 | 3/18/2023 |
1.7.2 | 874 | 3/8/2023 |
1.7.1-beta02 | 3,089 | 10/6/2022 |
1.7.1-beta01 | 193 | 10/6/2022 |
1.6.1 | 6,153 | 10/5/2022 |
1.6.0 | 2,878 | 10/2/2022 |
1.5.2 | 435 | 10/1/2022 |
1.5.1 | 429 | 10/1/2022 |
1.5.0 | 121,112 | 12/22/2019 |
1.4.1 | 541 | 12/22/2019 |
1.4.0 | 519 | 12/22/2019 |
1.3.0 | 530 | 12/22/2019 |
1.2.0.30 | 505 | 12/22/2019 |
1.1.0 | 542 | 12/22/2019 |
1.0.6345.28705 | 4,404 | 5/16/2017 |
Enable Source Link.