AssignAll 1.7.1-beta02
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
<PackageReference Include="AssignAll" Version="1.7.1-beta02" />
paket add AssignAll --version 1.7.1-beta02
#r "nuget: AssignAll, 1.7.1-beta02"
// 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
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 (>= 3.3.3)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.1.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 | 42,636 | 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,908 | 10/2/2022 |
1.5.2 | 464 | 10/1/2022 |
1.5.1 | 459 | 10/1/2022 |
1.5.0 | 137,446 | 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 |