Skip to content

Commit

Permalink
Merge pull request #331 from atc-net/feature/maintenance
Browse files Browse the repository at this point in the history
Feature/maintenance
  • Loading branch information
davidkallesen authored Oct 10, 2024
2 parents 4abfdba + bc1bf9e commit ebe150a
Show file tree
Hide file tree
Showing 44 changed files with 593 additions and 376 deletions.
40 changes: 38 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ATC coding rules - https://github.com/atc-net/atc-coding-rules
# Version: 1.0.0
# Updated: 25-09-2023
# Version: 1.0.1
# Updated: 03-06-2024
# Location: Root
# Distribution: DotNet8
# Inspired by: https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options
Expand Down Expand Up @@ -464,10 +464,46 @@ dotnet_diagnostic.MA0048.severity = error # https://github.com/atc-net
dotnet_diagnostic.CA1014.severity = none # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/CA1014.md
dotnet_diagnostic.CA1068.severity = error # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/CA1068.md
dotnet_diagnostic.CA1305.severity = error
dotnet_diagnostic.CA1308.severity = suggestion # Normalize strings to uppercase
dotnet_diagnostic.CA1510.severity = suggestion # Use ArgumentNullException throw helper
dotnet_diagnostic.CA1511.severity = suggestion # Use ArgumentException throw helper
dotnet_diagnostic.CA1512.severity = suggestion # Use ArgumentOutOfRangeException throw helper
dotnet_diagnostic.CA1513.severity = suggestion # Use ObjectDisposedException throw helper
dotnet_diagnostic.CA1514.severity = error # Avoid redundant length argument
dotnet_diagnostic.CA1707.severity = error # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/CA1707.md
dotnet_diagnostic.CA1812.severity = none
dotnet_diagnostic.CA1822.severity = suggestion
dotnet_diagnostic.CA1849.severity = error # Call async methods when in an async method
dotnet_diagnostic.CA1854.severity = suggestion # Prefer the 'IDictionary.TryGetValue(TKey, out TValue)' method
dotnet_diagnostic.CA1855.severity = suggestion # Prefer 'Clear' over 'Fill'
dotnet_diagnostic.CA1856.severity = error # Incorrect usage of ConstantExpected attribute
dotnet_diagnostic.CA1857.severity = suggestion # A constant is expected for the parameter
dotnet_diagnostic.CA1858.severity = suggestion # Use 'StartsWith' instead of 'IndexOf'
dotnet_diagnostic.CA1859.severity = suggestion # Use concrete types when possible for improved performance
dotnet_diagnostic.CA1860.severity = suggestion # Avoid using 'Enumerable.Any()' extension method
dotnet_diagnostic.CA1861.severity = suggestion # Avoid constant arrays as arguments
dotnet_diagnostic.CA1862.severity = error # Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
dotnet_diagnostic.CA1863.severity = suggestion # Use 'CompositeFormat'
dotnet_diagnostic.CA1864.severity = suggestion # Prefer the 'IDictionary.TryAdd(TKey, TValue)' method
dotnet_diagnostic.CA1865.severity = suggestion # Use char overload
dotnet_diagnostic.CA1866.severity = suggestion # Use char overload
dotnet_diagnostic.CA1867.severity = suggestion # Use char overload
dotnet_diagnostic.CA1868.severity = suggestion # Unnecessary call to 'Contains(item)'
dotnet_diagnostic.CA1869.severity = suggestion # Cache and reuse 'JsonSerializerOptions' instances
dotnet_diagnostic.CA1870.severity = suggestion # Use a cached 'SearchValues' instance
dotnet_diagnostic.CA2007.severity = suggestion # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/CA2007.md
dotnet_diagnostic.CA2017.severity = error # Parameter count mismatch
dotnet_diagnostic.CA2018.severity = error # The count argument to Buffer.BlockCopy should specify the number of bytes to copy
dotnet_diagnostic.CA2019.severity = error # ThreadStatic fields should not use inline initialization
dotnet_diagnostic.CA2021.severity = error # Don't call Enumerable.Cast<T> or Enumerable.OfType<T> with incompatible types
dotnet_diagnostic.CA2250.severity = suggestion # Use ThrowIfCancellationRequested
dotnet_diagnostic.CA2252.severity = suggestion # Opt-in to preview features should be used with caution
dotnet_diagnostic.CA2253.severity = error # Named placeholders should not be numeric values
dotnet_diagnostic.CA2254.severity = suggestion # Template should be a static expression
dotnet_diagnostic.CA2255.severity = suggestion # The ModuleInitializer attribute should not be used in libraries
dotnet_diagnostic.CA2259.severity = error # Ensure ThreadStatic is only used with static fields
dotnet_diagnostic.CA2260.severity = error # Implement generic math interfaces correctly
dotnet_diagnostic.CA2261.severity = error # Do not use ConfigureAwaitOptions.SuppressThrowing with Task<TResult>
dotnet_diagnostic.IDE0005.severity = warning # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/IDE0005.md
dotnet_diagnostic.IDE0058.severity = none # https://github.com/atc-net/atc-coding-rules/blob/main/documentation/CodeAnalyzersRules/MicrosoftCodeAnalysis/IDE0058.md

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<ItemGroup Label="Code Analyzers">
<PackageReference Include="AsyncFixer" Version="1.6.0" PrivateAssets="All" />
<PackageReference Include="Asyncify" Version="0.9.7" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.163" PrivateAssets="All" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.169" PrivateAssets="All" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" PrivateAssets="All" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="All" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="9.32.0.97167" PrivateAssets="All" />
Expand Down
19 changes: 15 additions & 4 deletions docs/CodeDoc/Atc/Atc.Helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -1945,16 +1945,27 @@ ReflectionHelper.
### Static Methods
#### GetPrivateField
>```csharp
>T GetPrivateField(object target, string fieldName)
>```
><b>Summary:</b> Gets the value of a private field from the specified target object.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`target`&nbsp;&nbsp;-&nbsp;&nbsp;The target object containing the private field.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`fieldName`&nbsp;&nbsp;-&nbsp;&nbsp;The name of the private field to retrieve.<br />
>
><b>Returns:</b> The value of the private field, cast to the specified type.
#### SetPrivateField
>```csharp
>void SetPrivateField(object target, string fieldName, object value)
>```
><b>Summary:</b> Sets the private field.
><b>Summary:</b> Sets the value of a private field on the specified target object.
>
><b>Parameters:</b><br>
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`target`&nbsp;&nbsp;-&nbsp;&nbsp;The target.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`fieldName`&nbsp;&nbsp;-&nbsp;&nbsp;Name of the field.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`value`&nbsp;&nbsp;-&nbsp;&nbsp;The value.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`target`&nbsp;&nbsp;-&nbsp;&nbsp;The target object containing the private field.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`fieldName`&nbsp;&nbsp;-&nbsp;&nbsp;The name of the private field to set.<br />
>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`value`&nbsp;&nbsp;-&nbsp;&nbsp;The value to set on the private field.<br />
<br />
Expand Down
1 change: 1 addition & 0 deletions docs/CodeDoc/Atc/IndexExtended.md
Original file line number Diff line number Diff line change
Expand Up @@ -4669,6 +4669,7 @@
- KillEntryCaller(int timeoutInSec = 30)
- [ReflectionHelper](Atc.Helpers.md#reflectionhelper)
- Static Methods
- GetPrivateField(object target, string fieldName)
- SetPrivateField(object target, string fieldName, object value)
- [RegionInfoHelper](Atc.Helpers.md#regioninfohelper)
- Static Methods
Expand Down
5 changes: 5 additions & 0 deletions sample/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
# StyleCop
# https://github.com/DotNetAnalyzers/StyleCopAnalyzers

# SonarAnalyzer.CSharp
# https://rules.sonarsource.com/csharp

dotnet_diagnostic.S1075.severity = none # Refactor your code not to use hardcoded absolute paths or URIs


##########################################
# Custom - Code Analyzers Rules
Expand Down
25 changes: 25 additions & 0 deletions sample/Demo.Atc.Console.Spectre.Cli/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ATC coding rules - https://github.com/atc-net/atc-coding-rules
# Version: 1.0.0
# Updated: 11-04-2024
# Location: cli
# Distribution: Frameworks

##########################################
# Code Analyzers Rules
##########################################
[*.{cs}]

dotnet_diagnostic.CA1031.severity = none # Do not catch general exception types
dotnet_diagnostic.CA1303.severity = none # Do not pass literals as localized parameters
dotnet_diagnostic.CA1819.severity = none # Properties should not return arrays
dotnet_diagnostic.CA1848.severity = none # Use the LoggerMessage delegates
dotnet_diagnostic.CA2000.severity = none # Dispose objects before losing scope
dotnet_diagnostic.CA2254.severity = none # Template should be a static expression

dotnet_diagnostic.MA0076.severity = none # Do not use implicit culture-sensitive ToString in interpolated strings

dotnet_diagnostic.S2629.severity = none # Don't use string interpolation in logging message templates.

##########################################
# Custom - Code Analyzers Rules
##########################################
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
</ItemGroup>
Expand Down
25 changes: 25 additions & 0 deletions sample/Demo.Atc.Dotnet.Cli/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ATC coding rules - https://github.com/atc-net/atc-coding-rules
# Version: 1.0.0
# Updated: 11-04-2024
# Location: cli
# Distribution: Frameworks

##########################################
# Code Analyzers Rules
##########################################
[*.{cs}]

dotnet_diagnostic.CA1031.severity = none # Do not catch general exception types
dotnet_diagnostic.CA1303.severity = none # Do not pass literals as localized parameters
dotnet_diagnostic.CA1819.severity = none # Properties should not return arrays
dotnet_diagnostic.CA1848.severity = none # Use the LoggerMessage delegates
dotnet_diagnostic.CA2000.severity = none # Dispose objects before losing scope
dotnet_diagnostic.CA2254.severity = none # Template should be a static expression

dotnet_diagnostic.MA0076.severity = none # Do not use implicit culture-sensitive ToString in interpolated strings

dotnet_diagnostic.S2629.severity = none # Don't use string interpolation in logging message templates.

##########################################
# Custom - Code Analyzers Rules
##########################################
2 changes: 1 addition & 1 deletion sample/Demo.Atc.Dotnet.Cli/Demo.Atc.Dotnet.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Atc.CodeAnalysis.CSharp/Atc.CodeAnalysis.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Atc.Console.Spectre/Atc.Console.Spectre.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageReference Include="Spectre.Console" Version="0.49.1" />
<PackageReference Include="Spectre.Console.Cli" Version="0.49.1" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Atc.OpenApi/Atc.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.17" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.22" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Atc.Rest.Extended/Atc.Rest.Extended.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.22.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.8" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.8.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="FluentAssertions" Version="6.12.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Atc.Rest.HealthChecks/Atc.Rest.HealthChecks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="8.0.10" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Atc.XUnit/Atc.XUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EPPlus" Version="7.2.2" />
<PackageReference Include="EPPlus" Version="7.4.0" />
<PackageReference Include="ICSharpCode.Decompiler" Version="8.2.0.7535" />
<PackageReference Include="Mono.Reflection" Version="2.0.0">
<NoWarn>NU1701</NoWarn>
</PackageReference>
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit" Version="2.9.2" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Atc/Atc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Polyfill" Version="1.0.39">
<PackageReference Include="Meziantou.Polyfill" Version="1.0.40">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.2" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
</ItemGroup>

<ItemGroup>
Expand Down
80 changes: 64 additions & 16 deletions src/Atc/Helpers/ReflectionHelper.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ReSharper disable ReturnTypeCanBeNotNullable
namespace Atc.Helpers;

/// <summary>
Expand All @@ -6,11 +7,11 @@ namespace Atc.Helpers;
public static class ReflectionHelper
{
/// <summary>
/// Sets the private field.
/// Sets the value of a private field on the specified target object.
/// </summary>
/// <param name="target">The target.</param>
/// <param name="fieldName">Name of the field.</param>
/// <param name="value">The value.</param>
/// <param name="target">The target object containing the private field.</param>
/// <param name="fieldName">The name of the private field to set.</param>
/// <param name="value">The value to set on the private field.</param>
[SuppressMessage("Major Code Smell", "S3011:Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "OK.")]
[ExcludeFromCodeCoverage]
public static void SetPrivateField(object target, string fieldName, object value)
Expand All @@ -22,27 +23,74 @@ public static void SetPrivateField(object target, string fieldName, object value

if (string.IsNullOrEmpty(fieldName))
{
throw new ArgumentNullOrDefaultException(nameof(fieldName));
throw new ArgumentException("Field name cannot be null or empty", nameof(fieldName));
}

var fieldInfo = GetFieldInfo(target, fieldName);
if (fieldInfo is null)
{
throw new ArgumentPropertyException($"Field '{fieldName}' not found in type '{target.GetType()}' or its base types.");
}

fieldInfo.SetValue(target, value);
}

/// <summary>
/// Gets the value of a private field from the specified target object.
/// </summary>
/// <typeparam name="T">The type of the field value.</typeparam>
/// <param name="target">The target object containing the private field.</param>
/// <param name="fieldName">The name of the private field to retrieve.</param>
/// <returns>The value of the private field, cast to the specified type.</returns>
[SuppressMessage("Major Code Smell", "S3011:Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "OK.")]
[ExcludeFromCodeCoverage]
public static T? GetPrivateField<T>(object target, string fieldName)
{
if (target is null)
{
throw new ArgumentNullException(nameof(target));
}

if (string.IsNullOrEmpty(fieldName))
{
throw new ArgumentException("Field name cannot be null or empty", nameof(fieldName));
}

var fieldInfo = GetFieldInfo(target, fieldName);
if (fieldInfo is null)
{
throw new ArgumentPropertyException($"Field '{fieldName}' not found in type '{target.GetType()}' or its base types.");
}

if (fieldInfo.GetValue(target) is not T fieldValue)
{
throw new InvalidCastException($"Field '{fieldName}' found in type '{target.GetType()}', but cannot be cast to type '{typeof(T)}'.");
}

return fieldValue;
}

/// <summary>
/// Retrieves FieldInfo for a private field by name, traversing the inheritance hierarchy.
/// </summary>
/// <param name="target">The target object containing the private field.</param>
/// <param name="fieldName">The name of the private field.</param>
/// <returns>FieldInfo object representing the field, or null if not found.</returns>
[SuppressMessage("Major Code Smell", "S3011:Make sure that this accessibility bypass is safe here", Justification = "OK.")]
private static FieldInfo? GetFieldInfo(object target, string fieldName)
{
var type = target.GetType();
FieldInfo? fi = null;
while (type is not null)
{
fi = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
if (fi is not null)
var fieldInfo = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo is not null)
{
break;
return fieldInfo;
}

type = type.BaseType!;
}

if (fi is null)
{
throw new ArgumentPropertyException($"Field '{fieldName}' not found in type hierarchy.");
type = type.BaseType;
}

fi.SetValue(target, value);
return null;
}
}
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.141" PrivateAssets="All" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.6.143" PrivateAssets="All" />
</ItemGroup>

</Project>
Loading

0 comments on commit ebe150a

Please sign in to comment.