Skip to content

Commit

Permalink
Fixed usage of Contains, IndexOf, and LastIndexOf that didn't use Str…
Browse files Browse the repository at this point in the history
…ingComparison.Ordinal, to combat a possible breaking change in .NET 5.0. See: dotnet/runtime#43736
  • Loading branch information
dotnetjunkie committed Oct 27, 2020
1 parent 5efda85 commit 15bd6d8
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/SimpleInjector/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ where IsGenericImplementationOf(type, serviceType)
return name;
}

name = name.Contains("`") ? name.Substring(0, name.LastIndexOf('`')) : name;
name = name.IndexOf("`", StringComparison.Ordinal) > -1
? name.Substring(0, name.LastIndexOf("`", StringComparison.Ordinal))
: name;

return name + "<" + argumentsFormatter(genericArguments) + ">";
}
Expand Down

4 comments on commit 15bd6d8

@iamcarbon
Copy link

Choose a reason for hiding this comment

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

You can also do

IndexOf('`')

The char overload is always ordinal.

@dotnetjunkie
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for your comment. How are you so sure?

@iamcarbon
Copy link

@iamcarbon iamcarbon commented on 15bd6d8 Oct 31, 2020

Choose a reason for hiding this comment

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

The ordinal / culture-insensitive behavior is documented for the IndexOf(char) method under remarks. This is true for both .NET framework and .NET core. Only the IndexOf(string... ) methods are culture sensitive.

SEE --

https://docs.microsoft.com/en-us/dotnet/api/system.string.indexof?view=netcore-3.1#System_String_IndexOf_System_Char_

This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same.

@dotnetjunkie
Copy link
Collaborator Author

@dotnetjunkie dotnetjunkie commented on 15bd6d8 Nov 1, 2020

Choose a reason for hiding this comment

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

This method performs an ordinal (culture-insensitive) search, where a character is considered equivalent to another character only if their Unicode scalar values are the same.

Yes, but this seems to be changing in .NET Core 5.0, as this issue reports. That said, the "`" character might be unaffected, but better safe than sorry :-)

Please sign in to comment.