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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ccorsano
Copy link

@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
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<,>?

- 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 ec56684 to 778cb54 Compare March 7, 2024 22:05
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.

None yet

2 participants