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

Use base type for generic argument matching #1316

Merged
merged 6 commits into from
Mar 30, 2022
Merged

Conversation

tillig
Copy link
Member

@tillig tillig commented Mar 18, 2022

Fix #1315.

When you call GetInterfaces() on a Type, it returns the set of implemented interfaces but has already swapped the generic type argument names in for the ones used on the implementing Type. For example, if you have:

public interface IThing<T1> { }
public class Thing<TFirst> : IThing<TFirst> { }

...then when you call typeof(Thing<>).GetInterfaces() the result will be IThing<TFirst> - the name of the argument matches the implementer's name, not the one in the actual interface definition.

We use this naming to line up the type parameters on generic types and service type interface implementations.

For classes, that's not what we were doing.

public class BaseClass<T1> { }
public class DerivedClass<TFirst> : BaseClass<TFirst> { }

In the class case, we were basically doing typeof(DervivedClass<>).GetGenericArguments() and typeof(BaseClass<>).GetGenericArguments() and then trying to line up names. The problem is, the name had changed in the implementation so things wouldn't line up.

Instead of doing typeof(BaseClass<>).GetGenericArguments(), we need to walk back in the class hierarchy to find where DerivedClass<TFirst> actually derives from BaseClass<> so we can make use of the compiler having already renamed all the generic arguments for us... then we can match on name.

This PR does exactly that - walks back the inheritance hierarchy until it finds the derived class and then does the argument matching.

I added tests to show it'll work more than just one derived class deep, proving the updated name mapping works all the way down the stack.

@tillig
Copy link
Member Author

tillig commented Mar 18, 2022

Oh, I also found a typo in some of the test classes' namespaces Contraint instead of Constraint so I fixed that. The PR looks a little bigger than it actually is because of that.

@codecov
Copy link

codecov bot commented Mar 18, 2022

Codecov Report

Merging #1316 (8c2a9c2) into develop (66cce9f) will decrease coverage by 0.02%.
The diff coverage is 83.33%.

@@             Coverage Diff             @@
##           develop    #1316      +/-   ##
===========================================
- Coverage    76.83%   76.80%   -0.03%     
===========================================
  Files          188      188              
  Lines         5180     5191      +11     
  Branches      1061     1064       +3     
===========================================
+ Hits          3980     3987       +7     
- Misses         702      704       +2     
- Partials       498      500       +2     
Impacted Files Coverage Δ
.../Features/OpenGenerics/OpenGenericServiceBinder.cs 89.38% <83.33%> (-0.82%) ⬇️
src/Autofac/Util/SequenceGenerator.cs 71.42% <0.00%> (-28.58%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 66cce9f...8c2a9c2. Read the comment docs.

alistairjevans
alistairjevans previously approved these changes Mar 19, 2022
Copy link
Member

@alistairjevans alistairjevans left a comment

Choose a reason for hiding this comment

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

Approved, but with two minor queries if you're up for them. Generics are complicated....

{
// If it's not an interface, the implementation type MUST have derived from
// the generic service type at some point or there's no way to cast.
return Array.Empty<Type>();
Copy link
Member

Choose a reason for hiding this comment

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

Can we test this path?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know if we can unless something had gone buggy in the checks where we determine if the thing in RegisterGeneric and As are compatible but I can try walking backwards through it to see if we'd ever get there.

Copy link
Member

@alistairjevans alistairjevans left a comment

Choose a reason for hiding this comment

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

🐲

@alistairjevans alistairjevans merged commit 3402091 into develop Mar 30, 2022
@tillig
Copy link
Member Author

tillig commented Mar 30, 2022

Thanks! I couldn't figure out a good way to test that one code path yet. My gut says it'll never get hit. Not sure I'll have time to figure a test in the super near future, so good to not hold it up.

@tillig tillig deleted the issue-1315 branch March 30, 2022 06:10
@alistairjevans
Copy link
Member

Yeah, probably don't need a test for what is sort of an Assert statement that only trips if something else has gone horribly wrong.

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.

ComponentNotRegisteredException when type parameter name doesn't match the base class
2 participants