Skip to content

feat: convert string [Arguments] through declared TypeConverters - #7759

Merged
thomhurst merged 7 commits into
mainfrom
feat/typeconverter-arguments
Nov 14, 2026
Merged

thomhurst merged 7 commits into
mainfrom
feat/typeconverter-arguments

Conversation

@thomhurst

@thomhurst thomhurst commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Closes #7755

[Arguments("...")] now works for a parameter whose type declares a [TypeConverter] that converts from strings, including under Native AOT.

  • Analyzer: TUnit0001 no longer fires for a string argument when the parameter type (or a base type) has a usable [TypeConverter]. Nullable<T> is unwrapped. Both the typeof(X) and "Name, Assembly" forms are supported. The converter must be accessible, derive from TypeConverter, and have a public (Type) or parameterless constructor. The (Type) constructor is preferred, as TypeDescriptor does.
  • AOT: AotConverterGenerator registers a string -> T converter that calls new XConverter(...).ConvertFrom(null, InvariantCulture, value) directly. No TypeDescriptor, no reflection. Types that already convert from string through an operator keep using it.
  • Reflection mode: CastHelper falls back to TypeDescriptor.GetConverter for types with a declared [TypeConverter] when no generated converter is registered, for example when source generation is disabled. This is skipped in AOT mode.
  • Docs: added a "Converting String Arguments" section to arguments.md.

Only string arguments are handled. Whether a converter accepts other source types can't be known at compile time.

Testing

  • TUnit.Analyzers.Tests: all pass, including new tests: accepted (plain and nullable), rejected when the converter has no usable constructor, rejected for non-string arguments
  • TUnit.Core.SourceGenerator.Tests: all pass, including a new AotConverterScanTests case covering the emitted code for plain, (Type)-constructor and string-named converters, plus operator precedence
  • TUnit.TestProject TypeConverterArgumentsTests / TypeConverterClassArgumentsTests: pass in source-gen mode, in --reflection mode, and in a Native AOT publish (net10.0, osx-arm64). The publish produced no new trim or AOT warnings.
  • StringToParsableArgumentsTests and ArgumentWithImplicitConverterTests still pass in source-gen and reflection modes

Docs snippet compilation is left to CI.

Unrelated, found while testing: the existing StringToParsableArgumentsTests fail under Native AOT on main too. Filed as #7760.

Summary by CodeRabbit

  • New Features

    • String test arguments can be converted to parameter types with a compatible type converter, including nullable types and types that inherit a converter from a base type.
    • Conversions use invariant culture and are supported in Native AOT. When both parsing and a type converter are available, parsing takes precedence.
  • Documentation

    • Added guidance on string argument conversions, converter requirements, and examples.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: TypeConvertion be support

1 participant