A Roslyn analyzer for .NET Core projects that enforces specific coding standards and best practices.
This analyzer enforces that if and else control flow statements without braces can only contain:
returnstatementsthrowstatementscontinuestatementsbreakstatementsyield breakstatements
✅ Allowed (no braces needed):
if (condition)
return;
if (error)
throw new Exception();
while (true)
break;
foreach (var item in items)
continue;
if (done)
yield break;❌ Not allowed (braces required):
// This will trigger CT0001
if (condition)
DoSomething();This analyzer prevents assigning literal values to enum properties, requiring the use of proper enum values instead.
✅ Allowed:
AccountCategoryCode = AccountCategoryCode.Standard;❌ Not allowed:
// This will trigger CT0002
AccountCategoryCode = 1;This analyzer prevents the use of empty parentheses in object initialization when using object initializers.
✅ Allowed:
var account = new Account
{
Name = "MoneyMan",
};
var account = new Account(accountId)
{
Name = "MoneyMan",
};❌ Not allowed:
// This will trigger CT0003
var account = new Account()
{
Name = "MoneyMan",
};The analyzer is designed to be consumed as a project reference or NuGet package in other .NET projects. When integrated, it will automatically analyze your code and report violations of the rules.
dotnet build src\DataverseAnalyzer\DataverseAnalyzer.csproj --configuration ReleaseThe built analyzer DLL will be available in src\DataverseAnalyzer\bin\Release\netstandard2.0\DataverseAnalyzer.dll.
To use this analyzer in your projects, reference the built DLL as an analyzer:
<ItemGroup>
<Analyzer Include="path\to\DataverseAnalyzer.dll" />
</ItemGroup>The analyzer includes an automatic code fix provider that can add braces when violations are detected.