Skip to content

Commit 2e6ca60

Browse files
committed
review usage of string.Format in PresentationFramework
1 parent 408985a commit 2e6ca60

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+735
-766
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/LiveShapingList.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,8 @@ public DependencyProperty GetDP(string path)
732732
lock (s_Sync)
733733
{
734734
dp = DependencyProperty.RegisterAttached(
735-
String.Format(System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS,
736-
"LiveSortingTargetProperty{0}",
737-
s_dpList.Count),
735+
string.Create(System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS,
736+
$"LiveSortingTargetProperty{s_dpList.Count}"),
738737
typeof(object),
739738
typeof(LiveShapingList));
740739

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/ObjectRef.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,7 @@ internal override object GetObject(DependencyObject d, ObjectRefArgs args)
283283

284284
public override string ToString()
285285
{
286-
return String.Format(CultureInfo.InvariantCulture,
287-
"ElementName={0}", _name);
286+
return $"ElementName={_name}";
288287
}
289288

290289
internal override string Identify()
@@ -336,15 +335,11 @@ public override string ToString()
336335
switch (_relativeSource.Mode)
337336
{
338337
case RelativeSourceMode.FindAncestor:
339-
s = String.Format(CultureInfo.InvariantCulture,
340-
"RelativeSource {0}, AncestorType='{1}', AncestorLevel='{2}'",
341-
_relativeSource.Mode,
342-
_relativeSource.AncestorType,
343-
_relativeSource.AncestorLevel);
338+
s = string.Create(CultureInfo.InvariantCulture,
339+
$"RelativeSource {_relativeSource.Mode}, AncestorType='{_relativeSource.AncestorType}', AncestorLevel='{_relativeSource.AncestorLevel}'");
344340
break;
345341
default:
346-
s = String.Format(CultureInfo.InvariantCulture,
347-
"RelativeSource {0}", _relativeSource.Mode);
342+
s = string.Create(CultureInfo.InvariantCulture, $"RelativeSource {_relativeSource.Mode}");
348343
break;
349344
}
350345

@@ -453,8 +448,8 @@ protected override bool ProtectedUsesMentor
453448

454449
internal override string Identify()
455450
{
456-
return String.Format(System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS,
457-
"RelativeSource ({0})", _relativeSource.Mode);
451+
return string.Create(System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS,
452+
$"RelativeSource ({_relativeSource.Mode})");
458453
}
459454

460455
//------------------------------------------------------

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/PropertyPathWorker.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,16 +1270,13 @@ item is Freezable && // subproperty notifications only arise from Freezables
12701270
StringBuilder sb = new StringBuilder();
12711271

12721272
if (!Object.Equals(info, svs.info))
1273-
sb.AppendLine(String.Format(" Info is wrong: expected '{0}' got '{1}'",
1274-
info, svs.info));
1273+
sb.AppendLine($" Info is wrong: expected '{info}' got '{svs.info}'");
12751274

12761275
if (sourceType != svs.type)
1277-
sb.AppendLine(String.Format(" Type is wrong: expected '{0}' got '{1}'",
1278-
sourceType, svs.type));
1276+
sb.AppendLine($" Type is wrong: expected '{sourceType}' got '{svs.type}'");
12791277

12801278
if (item != BindingExpression.GetReference(svs.item))
1281-
sb.AppendLine(String.Format(" Item is wrong: expected '{0}' got '{1}'",
1282-
item, BindingExpression.GetReference(svs.item)));
1279+
sb.AppendLine($" Item is wrong: expected '{item}' got '{BindingExpression.GetReference(svs.item)}'");
12831280

12841281
int len1 = (args != null) ? args.Length : 0;
12851282
int len2 = (svs.args != null) ? svs.args.Length : 0;
@@ -1289,21 +1286,18 @@ item is Freezable && // subproperty notifications only arise from Freezables
12891286
{
12901287
if (!Object.Equals(args[i], svs.args[i]))
12911288
{
1292-
sb.AppendLine(String.Format(" args[{0}] is wrong: expected '{1}' got '{2}'",
1293-
i, args[i], svs.args[i]));
1289+
sb.AppendLine($" args[{i}] is wrong: expected '{args[i]}' got '{svs.args[i]}'");
12941290
}
12951291
}
12961292
}
12971293
else
1298-
sb.AppendLine(String.Format(" Args are wrong: expected length '{0}' got length '{1}'",
1299-
len1, len2));
1294+
sb.AppendLine($" Args are wrong: expected length '{len1}' got length '{len2}'");
13001295

1301-
if (sb.Length > 0)
1302-
{
1303-
Debug.Assert(false,
1304-
String.Format("Accessor cache returned incorrect result for ({0},{1},{2})\n{3}",
1305-
SVI[k].type, newType.Name, SVI[k].name, sb.ToString()));
1306-
}
1296+
Debug.Assert(sb.Length == 0,
1297+
$"""
1298+
Accessor cache returned incorrect result for ({SVI[k].type},{newType.Name},{SVI[k].name})
1299+
{sb.ToString()}
1300+
""");
13071301

13081302
return;
13091303
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/XmlBindingWorker.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,17 +551,15 @@ private string IdentifyNode(XmlNode node)
551551
if (node == null)
552552
return "<null>";
553553

554-
return String.Format(TypeConverterHelper.InvariantEnglishUS, "{0} ({1})",
555-
node.GetType().Name, node.Name);
554+
return $"{node.GetType().Name} ({node.Name})";
556555
}
557556

558557
private string IdentifyNodeList(XmlNodeList nodeList)
559558
{
560559
if (nodeList == null)
561560
return "<null>";
562561

563-
return String.Format(TypeConverterHelper.InvariantEnglishUS, "{0} (hash={1} Count={2})",
564-
nodeList.GetType().Name, AvTrace.GetHashCodeHelper(nodeList), nodeList.Count);
562+
return string.Create(TypeConverterHelper.InvariantEnglishUS, $"{nodeList.GetType().Name} (hash={AvTrace.GetHashCodeHelper(nodeList)} Count={nodeList.Count})");
565563
}
566564

567565
// 90% of the XPaths used in practice are very simple - consisting of a

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Globalization/BamlTreeMap.cs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
using MS.Utility;
2020

2121

22-
// Disabling 1634 and 1691:
23-
// In order to avoid generating warnings about unknown message numbers and
24-
// unknown pragmas when compiling C# source code with the C# compiler,
22+
// Disabling 1634 and 1691:
23+
// In order to avoid generating warnings about unknown message numbers and
24+
// unknown pragmas when compiling C# source code with the C# compiler,
2525
// you need to disable warnings 1634 and 1691. (Presharp Documentation)
2626
#pragma warning disable 1634, 1691
2727

@@ -82,7 +82,7 @@ internal InternalBamlLocalizabilityResolver Resolver
8282

8383
/// <summary>
8484
/// Maps a key to a baml tree node in the given tree
85-
/// </summary>
85+
/// </summary>
8686
internal BamlTreeNode MapKeyToBamlTreeNode(BamlLocalizableResourceKey key, BamlTree tree)
8787
{
8888
if (_keyToBamlNodeIndexMap.Contains(key))
@@ -95,7 +95,7 @@ internal BamlTreeNode MapKeyToBamlTreeNode(BamlLocalizableResourceKey key, BamlT
9595

9696
/// <summary>
9797
/// Maps a uid to a baml tree node in the given tree
98-
/// </summary>
98+
/// </summary>
9999
internal BamlStartElementNode MapUidToBamlTreeElementNode(string uid, BamlTree tree)
100100
{
101101
if (_uidToBamlNodeIndexMap.Contains(uid))
@@ -126,7 +126,7 @@ internal void EnsureMap()
126126
{
127127
BamlTreeNode currentNode = _tree[i];
128128

129-
// a node may be marked as unidentifiable if it or its parent has a duplicate uid.
129+
// a node may be marked as unidentifiable if it or its parent has a duplicate uid.
130130
if (currentNode.Unidentifiable) continue; // skip unidentifiable nodes
131131

132132
if (currentNode.NodeType == BamlNodeType.StartElement)
@@ -153,7 +153,7 @@ internal void EnsureMap()
153153
)
154154
);
155155

156-
// Mark this element and its properties unidentifiable.
156+
// Mark this element and its properties unidentifiable.
157157
currentNode.Unidentifiable = true;
158158
if (currentNode.Children != null)
159159
{
@@ -164,7 +164,7 @@ internal void EnsureMap()
164164
}
165165
}
166166

167-
continue; // skip the duplicate node
167+
continue; // skip the duplicate node
168168
}
169169
else
170170
{
@@ -179,7 +179,7 @@ internal void EnsureMap()
179179
&& currentNode.Parent != null
180180
&& currentNode.Parent.NodeType == BamlNodeType.StartDocument)
181181
{
182-
// remember the key to the root element so that
182+
// remember the key to the root element so that
183183
// users can further add modifications to the root that would have a global impact.
184184
// such as FlowDirection or CultureInfo
185185
_localizableResources.SetRootElementKey(key);
@@ -204,7 +204,7 @@ internal void EnsureMap()
204204
//-------------------------------------------------
205205

206206
/// <summary>
207-
/// Return the localizable resource key for this baml tree node.
207+
/// Return the localizable resource key for this baml tree node.
208208
/// If this node shouldn't be localized, the key returned will be null.
209209
/// </summary>
210210
internal static BamlLocalizableResourceKey GetKey(BamlTreeNode node)
@@ -241,15 +241,11 @@ internal static BamlLocalizableResourceKey GetKey(BamlTreeNode node)
241241
}
242242
else
243243
{
244-
// This node is auto-numbered. This has to do with the fact that
245-
// the compiler may compile duplicated properties into Baml under the same element.
246-
uid = string.Format(
244+
// This node is auto-numbered. This has to do with the fact that
245+
// the compiler may compile duplicated properties into Baml under the same element.
246+
uid = string.Create(
247247
TypeConverterHelper.InvariantEnglishUS,
248-
"{0}.{1}_{2}",
249-
parent.Uid,
250-
propertyNode.PropertyName,
251-
propertyNode.Index
252-
);
248+
$"{parent.Uid}.{propertyNode.PropertyName}_{propertyNode.Index}");
253249
}
254250

255251
key = new BamlLocalizableResourceKey(
@@ -310,7 +306,7 @@ internal class InternalBamlLocalizabilityResolver : BamlLocalizabilityResolver
310306
private Dictionary<string, LocalizabilityAttribute> _propertyAttributeTable;
311307

312308
//
313-
// cached localization comments
309+
// cached localization comments
314310
// Normally, we only need to use comments of a single element at one time.
315311
// In case of formatting inline tags we might be grabing comments of a few more elements.
316312
// A small cached would be enough
@@ -398,7 +394,7 @@ internal void ReleaseLocalizabilityCache()
398394
_commentsDocument = null;
399395
}
400396

401-
// Grab the comments on a BamlTreeNode for the value.
397+
// Grab the comments on a BamlTreeNode for the value.
402398
internal LocalizabilityGroup GetLocalizabilityComment(
403399
BamlStartElementNode node,
404400
string localName
@@ -423,7 +419,7 @@ internal string GetStringComment(
423419
string localName
424420
)
425421
{
426-
// get all the comments declares on this node
422+
// get all the comments declares on this node
427423
ElementComments comment = LookupCommentForElement(node);
428424
for (int i = 0; i < comment.LocalizationComments.Length; i++)
429425
{
@@ -443,14 +439,14 @@ internal void RaiseErrorNotifyEvent(BamlLocalizerErrorNotifyEventArgs e)
443439

444440
//--------------------------------------
445441
// BamlLocalizabilityResolver interface
446-
//--------------------------------------
442+
//--------------------------------------
447443
public override ElementLocalizability GetElementLocalizability(string assembly, string className)
448444
{
449445
if (_externalResolver == null
450446
|| assembly == null || assembly.Length == 0
451447
|| className == null || className.Length == 0)
452448
{
453-
// return the default value
449+
// return the default value
454450
return new ElementLocalizability(
455451
null,
456452
DefaultAttribute
@@ -616,7 +612,7 @@ private ElementComments LookupCommentForElement(BamlStartElementNode node)
616612
//
617613
// The baml itself might contain comments too
618614
// Grab the missing comments from Baml if there is any.
619-
//
615+
//
620616

621617
for (int i = 0;
622618
i < node.Children.Count && (comment.LocalizationComments.Length == 0 || comment.LocalizationAttributes.Length == 0);
@@ -636,14 +632,14 @@ private ElementComments LookupCommentForElement(BamlStartElementNode node)
636632
else if (LocComments.IsLocLocalizabilityProperty(propertyNode.OwnerTypeFullName, propertyNode.PropertyName)
637633
&& comment.LocalizationAttributes.Length == 0)
638634
{
639-
// grab comments from Baml
635+
// grab comments from Baml
640636
SetLocalizationAttributes(node, comment, propertyNode.Value);
641637
}
642638
}
643639
}
644640
}
645641

646-
// cached it
642+
// cached it
647643
_comments[_commentsIndex] = comment;
648644
_commentsIndex = (_commentsIndex + 1) % _comments.Length;
649645

@@ -652,8 +648,8 @@ private ElementComments LookupCommentForElement(BamlStartElementNode node)
652648

653649
private static XmlElement FindElementByID(XmlDocument doc, string uid)
654650
{
655-
// Have considered using XPATH. However, XPATH doesn't have a way to escape single quote within
656-
// single quotes, here we iterate through the document by ourselves
651+
// Have considered using XPATH. However, XPATH doesn't have a way to escape single quote within
652+
// single quotes, here we iterate through the document by ourselves
657653
if (doc != null && doc.DocumentElement != null)
658654
{
659655
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
@@ -722,13 +718,13 @@ string stringComment
722718
}
723719

724720
/// <summary>
725-
/// Data structure for all the comments declared on a particular element
721+
/// Data structure for all the comments declared on a particular element
726722
/// </summary>
727723
private class ElementComments
728724
{
729725
internal string ElementId; // element's uid
730726
internal PropertyComment[] LocalizationAttributes; // Localization.Attributes
731-
internal PropertyComment[] LocalizationComments; // Localization.Comments
727+
internal PropertyComment[] LocalizationComments; // Localization.Comments
732728

733729
internal ElementComments()
734730
{

0 commit comments

Comments
 (0)