[DependencyInjection] Use '==' instead of 'Equals()` to test service type equality - #90334
Conversation
|
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection Issue DetailsRelated issue: dotnet/aspnetcore#49485 It seems that scrutor uses a custom implementation of Unfortunately, this type override the Type t = typeof(object);
var decoratedType = new DecoratedType(t);
var result1 = t.Equals(decoratedType); // returns true
var result2 = decoratedType.Equals(t); // returns falseUsing
|
|
Seems like a design flaw on the Scrutor side, honestly |
| if (ServiceKey == null && other.ServiceKey == null) | ||
| { | ||
| return ServiceType.Equals(other.ServiceType); | ||
| return ServiceType == other.ServiceType; |
There was a problem hiding this comment.
operator== is overloaded for System.Type and it just forwards the call to Equals unless the type is RuntimeType: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Type.cs#L654-L666
Is it actually fixing anything?
There was a problem hiding this comment.
As long as you are trying to compare RuntimeType against DecoratedType at least.
The comment
// If `left` is a non-runtime type with a weird Equals implementation
// this is where operator `==` would differ from `Equals` call.seems to indicate that someone else already experienced that...
There was a problem hiding this comment.
Can we add a test for this?
@IDisposable How come? |
|
Maybe the fix is to compare |
That's the entire reason for the derived class. Scrutor used to do decoration by replacing existing This was solved by |
That's what Type t = typeof(object);
var decoratedType = new DecoratedType(t);
var result1 = t.Equals(decoratedType); // returns false
var result2 = decoratedType.Equals(t); // returns falseIs what you want. |
Seems like the wrapper class that Scrutor is generating having only reference-equality is what's wrong here... the class is proxying/wrapping for some other class representing type, if it can't enforce the identity (e.g. always handing out the same instance for the underlying/wrapped type) then it shouldn't be enforcing reference identity. |
steveharter
left a comment
There was a problem hiding this comment.
A test would be nice, but not blocking.
The CI Build failure is related to the following: - khellang/Scrutor#208 - dotnet/runtime#90334
Related issue: dotnet/aspnetcore#49485
It seems that scrutor uses a custom implementation of
Type: https://github.com/khellang/Scrutor/blob/master/src/Scrutor/DecoratedType.cs#L15Unfortunately, this type override the
Equals()method, making it non "symetrical" withType:Using
==to test if two types are the same seems to fix the issue.