You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
string.Copy deprecation removes a necessary use case with TextRenderer.MeasureText #1564
design-discussionOngoing discussion about design without consensustenet-compatibilityIncompatibility with previous versions or with WinForms for .NET Framework
Have you experienced this same bug with .NET Framework?: No
Problem description:
string.Copy was deprecated in .NET Core 3.0 (for good reason), however, I had been using it with TextRenderer.MeasureText, which modifies the string when calling it if the string is too long for the width specified. Without string.Copy, I'm not sure how else you would use this API without string.Copy for now. With the warning showing that this API was removed as strings may be deduplicated in the future, I wasn't sure about how to properly change this code to be more future-proof, as I'm not sure it can be in its current state.
Minimal repro:
usingSystem;usingSystem.Drawing;usingSystem.Windows.Forms;namespaceConsoleApp10{classProgram{staticvoidMain(string[]args){stringoriginal=@"C:\New Folder\New Folder\New Folder\New Folder\New Folder\New Folder\text.txt";stringresult=GetCompactedString(original,4);//Result is now correctly "...\text.txt"//Expectation is that original is still the very long file path, but it is the same as result}publicstaticstringGetCompactedString(stringstringToCompact,intmaxWidth){//This line is necessary to not cause the bug shown below//string copy= string.Copy(stringToCompact);stringcopy=stringToCompact;varopts=TextFormatFlags.PathEllipsis|TextFormatFlags.ModifyString;TextRenderer.MeasureText(copy,newFont("Segoe UI",9),newSize(maxWidth,0),opts);// https://connect.microsoft.com/VisualStudio/feedback/details/620330intnullByteLoc=copy.IndexOf('\0');if(nullByteLoc!=-1){copy=copy.Substring(0,nullByteLoc);}returncopy;}}}
That sounds more like a bug in the MeasureText implementation than an intended feature, but nice catch anyways. I suspect fixing this needs a new overload though, I don't think the function should go around and break runtime invariants by writing into an immutable object.
I think we are beyond the timeframe where we can add an overload for 3.0 and 3.1 so we will probably need to fix this issue in 5.0. Moving to that milestone for now. We can always bring it back if we have the time to fix it.
Like said above TextRenderer should probably filter out or throw on passing the ModifyString flag and have a separate API for mutable strings (i.e. StringBuilder, ref string or mutable span)
design-discussionOngoing discussion about design without consensustenet-compatibilityIncompatibility with previous versions or with WinForms for .NET Framework
Activity
weltkante commentedon Aug 2, 2019
That sounds more like a bug in the MeasureText implementation than an intended feature, but nice catch anyways. I suspect fixing this needs a new overload though, I don't think the function should go around and break runtime invariants by writing into an immutable object.
merriemcgaw commentedon Aug 9, 2019
I think we are beyond the timeframe where we can add an overload for 3.0 and 3.1 so we will probably need to fix this issue in 5.0. Moving to that milestone for now. We can always bring it back if we have the time to fix it.
zsd4yr commentedon Aug 9, 2019
It is not as simple as
string.Copy, but can you just?:or
JeremyKuhne commentedon Jul 28, 2020
Need to validate we don't allow corrupting the immutability of strings.
weltkante commentedon Jul 28, 2020
Like said above TextRenderer should probably filter out or throw on passing the
ModifyStringflag and have a separate API for mutable strings (i.e. StringBuilder, ref string or mutable span)JeremyKuhne commentedon Sep 9, 2020
#3710 tracks replacing the existing
TextRendererAPI with a safe alternative.