Visitar URL original
TryParseEnum - Apply Enum.TryParse<T> since better for AOT by snakefoot · Pull Request #6140 · NLog/NLog · GitHub
Skip to content

TryParseEnum - Apply Enum.TryParse<T> since better for AOT - #6140

Merged
snakefoot merged 1 commit into
NLog:devfrom
snakefoot:TryParseEnumV2
Apr 4, 2026
Merged

snakefoot merged 1 commit into
NLog:devfrom
snakefoot:TryParseEnumV2

Conversation

@snakefoot

Copy link
Copy Markdown
Contributor

No description provided.

@coderabbitai

coderabbitai Bot commented Apr 4, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 099b38f4-21c8-44b5-87bc-cc57d3ec45cf

📥 Commits

Reviewing files that changed from the base of the PR and between 8850a89 and e4249c0.

📒 Files selected for processing (2)
  • src/NLog/Common/ConversionHelpers.cs
  • tests/NLog.UnitTests/Internal/ConversionHelpersTests.cs
✅ Files skipped from review due to trivial changes (1)
  • tests/NLog.UnitTests/Internal/ConversionHelpersTests.cs

Walkthrough

Explicit enum-type validation was added to the non-NETSTANDARD2_1_OR_GREATER/non-NET TryParseEnum(string, Type, out object?) path, and the generic TryParseEnum<TEnum> compile-time conditions were adjusted to exclude NET40, routing it through the fallback implementation.

Changes

Cohort / File(s) Summary
Enum Parsing Validation
src/NLog/Common/ConversionHelpers.cs
Added explicit enumType.IsEnum check and ArgumentException throw in the non-NETSTANDARD2_1_OR_GREATER/non-NET TryParseEnum(string, Type, out object?) fallback; changed compile-time condition from #if !NET35 to #if !NET35 && !NET40 in TryParseEnum<TEnum>() so NET40 uses fallback logic.
Test Coverage
tests/NLog.UnitTests/Internal/ConversionHelpersTests.cs
Added assertion in EnumParse_ArgumentException_ignoreCaseTrue to verify TryParseEnum("not enum", typeof(double), out _) throws ArgumentException.

Estimated Code Review Effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 I hopped through compile-time leaves and found,
a check that keeps enums safe and sound,
NET40 now follows the safer trail,
No more surprises in parsing detail! ✨

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive No pull request description was provided by the author, so there is no description to evaluate against the changeset. Add a brief description explaining the rationale for preferring Enum.TryParse over the non-generic variant for AOT compatibility.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: switching to Enum.TryParse for better AOT (Ahead-of-Time) compilation support.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/NLog/Common/ConversionHelpers.cs`:
- Around line 114-115: The preprocessor guard in ConversionHelpers.cs excludes
NET40 without explanation; either remove NET40 from the `#if` condition so
Enum.TryParse<TEnum>(inputValue, ignoreCase, out resultValue) is used on .NET
4.0, or add a one-line inline comment immediately above the conditional
explaining why NET40 must fall back (e.g., specific AOT/reflection limitation or
runtime bug) and reference the conditional/Enum.TryParse usage and the
resultValue variable so reviewers can locate the code.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 0aa2fee6-4af6-47e9-9977-492b35bbc9d6

📥 Commits

Reviewing files that changed from the base of the PR and between 8ba58a4 and 8850a89.

📒 Files selected for processing (2)
  • src/NLog/Common/ConversionHelpers.cs
  • tests/NLog.UnitTests/Internal/ConversionHelpersTests.cs

Comment thread src/NLog/Common/ConversionHelpers.cs Resolved
@sonarqubecloud

sonarqubecloud Bot commented Apr 4, 2026

Copy link
Copy Markdown

@sonarqubecloud

sonarqubecloud Bot commented Apr 4, 2026

Copy link
Copy Markdown

@snakefoot
snakefoot merged commit e0dd99d into NLog:dev Apr 4, 2026
6 of 7 checks passed
@snakefoot snakefoot added this to the 6.1.3 milestone May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant