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

ParameterInfo.ToString() is inconsistent with the .NET Framework behavior #110447

Open
enyim opened this issue Dec 5, 2024 · 2 comments
Open
Labels
area-System.Reflection needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration

Comments

@enyim
Copy link

enyim commented Dec 5, 2024

Description

I'm porting large amount of legacy code that relies on string comparison of various objects collected via reflection. (Code should run both on .NET Framework and .NET 8.0+)

Part of this code uses ParameterInfo.ToString() that has different behavior between the frameworks. (Changed in #52804, commit 0377084)

4.8 did not distinguish between null and "" Name, while 6+ does. This in itself would not be a problem, as we are not comparing data between frameworks. However, .NET 8 sometimes returns null, sometimes "" for MethodInfo.ReturnParameter.Name, which, with the new behavior breaks tests.

Yes, one could argue that ToString() output is not something to rely on, but

  1. there is already large amount of code out there that do rely on it & I believe backward compatibility is more important
  2. ToString() in the Reflection APIs (mostly?) still behave as in .NET Framework

Reproduction Steps

See the attached repro project. Build and run both versions.

ParamInfo.zip

net48:

Name = null => "System.Object "
Name = String.Empty => "System.Object "
Are they equal? True

System.Object EndInvoke(System.IAsyncResult)
  ReturnParameter.ToString() = "System.Object "
  Name = null
System.Object EndInvoke(System.IAsyncResult)
  ReturnParameter.ToString() = "System.Object "
  Name = null

net8.0

Name = null => "System.Object"
Name = String.Empty => "System.Object "
Are they equal? False

System.Object EndInvoke(System.IAsyncResult)
  ReturnParameter.ToString() = "System.Object "
  Name = ""
System.Object EndInvoke(System.IAsyncResult)
  ReturnParameter.ToString() = "System.Object"
  Name = null

Expected behavior

ParameterInfo should not distinguish between null and empty names, as both values mean that it does not have a name.

Actual behavior

Difference in behavior.

Regression?

Changed behavior from .NET Framework 4.8

Does not seem to be documented in https://learn.microsoft.com/en-us/dotnet/core/compatibility/6.0

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 5, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Dec 5, 2024
@teo-tsirpanis teo-tsirpanis added area-System.Reflection and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 5, 2024
@JuergenAuer
Copy link

Hi @enyim

Name = null => "System.Object"
Name = String.Empty => "System.Object "
Are they equal? False

first is "System.Object", second is "System.Object " with a space at the end.

Different strings, not the same.

Your NET.4.8 sample - both with a space. Your NET.8: One with, one without a space.

@steveharter steveharter added needs-author-action An issue or pull request that requires more info or actions from the author. and removed untriaged New issue has not been triaged by the area owner labels Dec 5, 2024
@enyim
Copy link
Author

enyim commented Dec 9, 2024

Hi @enyim

Name = null => "System.Object"
Name = String.Empty => "System.Object "
Are they equal? False

first is "System.Object", second is "System.Object " with a space at the end.

Different strings, not the same.

Your NET.4.8 sample - both with a space. Your NET.8: One with, one without a space.

yes, this is the issue I am reporting.

@dotnet-policy-service dotnet-policy-service bot added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Reflection needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Projects
None yet
Development

No branches or pull requests

4 participants