Repository navigation
feat: convert string [Arguments] through declared TypeConverters - #7759
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #7755
[Arguments("...")]now works for a parameter whose type declares a[TypeConverter]that converts from strings, including under Native AOT.[TypeConverter].Nullable<T>is unwrapped. Both thetypeof(X)and"Name, Assembly"forms are supported. The converter must be accessible, derive fromTypeConverter, and have a public(Type)or parameterless constructor. The(Type)constructor is preferred, asTypeDescriptordoes.AotConverterGeneratorregisters astring -> Tconverter that callsnew XConverter(...).ConvertFrom(null, InvariantCulture, value)directly. NoTypeDescriptor, no reflection. Types that already convert from string through an operator keep using it.CastHelperfalls back toTypeDescriptor.GetConverterfor types with a declared[TypeConverter]when no generated converter is registered, for example when source generation is disabled. This is skipped in AOT mode.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 argumentsTUnit.Core.SourceGenerator.Tests: all pass, including a newAotConverterScanTestscase covering the emitted code for plain,(Type)-constructor and string-named converters, plus operator precedenceTUnit.TestProjectTypeConverterArgumentsTests/TypeConverterClassArgumentsTests: pass in source-gen mode, in--reflectionmode, and in a Native AOT publish (net10.0, osx-arm64). The publish produced no new trim or AOT warnings.StringToParsableArgumentsTestsandArgumentWithImplicitConverterTestsstill pass in source-gen and reflection modesDocs snippet compilation is left to CI.
Unrelated, found while testing: the existing
StringToParsableArgumentsTestsfail under Native AOT onmaintoo. Filed as #7760.Summary by CodeRabbit
New Features
Documentation