Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support implementing multiple IConverter<,> in single converter class #8881

Merged
merged 5 commits into from
Jul 8, 2024

Conversation

ccorsano
Copy link
Contributor

@ccorsano ccorsano commented Feb 25, 2024

Fix and unit test for #8879

Only select IConverter<,> interfaces whose types arguments matches the searched type.

Unit test also submitted separately as a fail test in #8880

Microsoft Reviewers: Open in CodeFlow
@@ -570,7 +570,8 @@ private bool TryGetSurrogateCodec(Type fieldType, Type searchType, out Type surr
var converterInterfaceArgs = Array.Empty<Type>();
foreach (var @interface in converterType.GetInterfaces())
{
if (@interface.IsConstructedGenericType && @interface.GetGenericTypeDefinition() == typeof(IConverter<,>))
if (@interface.IsConstructedGenericType && @interface.GetGenericTypeDefinition() == typeof(IConverter<,>)
&& @interface.GenericTypeArguments[0].IsAssignableFrom(fieldType))
Copy link
Member

Choose a reason for hiding this comment

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

Should this be an equality check instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes a simpler equality check should be fine.

@ccorsano ccorsano force-pushed the multiple_surrogate_in_converter branch from 3fd4ac4 to ec56684 Compare March 7, 2024 19:35
@ReubenBond
Copy link
Member

Should we also add IPopulator<,>?

@ReubenBond ReubenBond force-pushed the multiple_surrogate_in_converter branch from ec56684 to 778cb54 Compare March 7, 2024 22:05
ccorsano and others added 5 commits July 8, 2024 12:40
- When declaring a single converter class for multiple surrogates,
  ICopier will always resolve the last declared IConverter<,> interface,
  failing with a InvalidCastException at runtime
- Added Equals and GetHashCode to foreign types
- Only resolves IConverter<,> interface matching foreign type
@ReubenBond ReubenBond force-pushed the multiple_surrogate_in_converter branch from 42b23df to 9de0fd6 Compare July 8, 2024 19:41
@ReubenBond ReubenBond merged commit 817eb5e into dotnet:main Jul 8, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants