@@ -114,10 +114,8 @@ internal ProjectItemInstance(ProjectInstance project, string itemType, string in
114
114
if ( directMetadata ? . GetEnumerator ( ) . MoveNext ( ) == true )
115
115
{
116
116
metadata = new CopyOnWritePropertyDictionary < ProjectMetadataInstance > ( ) ;
117
- foreach ( KeyValuePair < string , string > metadatum in directMetadata )
118
- {
119
- metadata . Set ( new ProjectMetadataInstance ( metadatum . Key , metadatum . Value ) ) ;
120
- }
117
+ IEnumerable < ProjectMetadataInstance > directMetadataInstances = directMetadata . Select ( metadatum => new ProjectMetadataInstance ( metadatum . Key , metadatum . Value ) ) ;
118
+ metadata . ImportProperties ( directMetadataInstances ) ;
121
119
}
122
120
123
121
CommonConstructor ( project , itemType , includeEscaped , includeEscaped , metadata , null /* need to add item definition metadata */ , definingFileEscaped ) ;
@@ -587,11 +585,10 @@ void ITranslatable.Translate(ITranslator translator)
587
585
internal static void SetMetadata ( IEnumerable < KeyValuePair < string , string > > metadataList , IEnumerable < ProjectItemInstance > items )
588
586
{
589
587
// Set up a single dictionary that can be applied to all the items
590
- CopyOnWritePropertyDictionary < ProjectMetadataInstance > metadata = new CopyOnWritePropertyDictionary < ProjectMetadataInstance > ( ) ;
591
- foreach ( KeyValuePair < string , string > metadatum in metadataList )
592
- {
593
- metadata . Set ( new ProjectMetadataInstance ( metadatum . Key , metadatum . Value ) ) ;
594
- }
588
+ CopyOnWritePropertyDictionary < ProjectMetadataInstance > metadata = new ( ) ;
589
+
590
+ IEnumerable < ProjectMetadataInstance > projectMetadataInstances = metadataList . Select ( metadatum => new ProjectMetadataInstance ( metadatum . Key , metadatum . Value ) ) ;
591
+ metadata . ImportProperties ( projectMetadataInstances ) ;
595
592
596
593
foreach ( ProjectItemInstance item in items )
597
594
{
@@ -1096,40 +1093,45 @@ internal CopyOnWritePropertyDictionary<ProjectMetadataInstance> MetadataCollecti
1096
1093
1097
1094
CopyOnWritePropertyDictionary < ProjectMetadataInstance > allMetadata = new CopyOnWritePropertyDictionary < ProjectMetadataInstance > ( ) ;
1098
1095
1099
- // Next, any inherited item definitions. Front of the list is highest priority,
1100
- // so walk backwards.
1101
- for ( int i = _itemDefinitions . Count - 1 ; i >= 0 ; i -- )
1096
+ allMetadata . ImportProperties ( metaData ( ) ) ;
1097
+
1098
+ return allMetadata ;
1099
+
1100
+ IEnumerable < ProjectMetadataInstance > metaData ( )
1102
1101
{
1103
- foreach ( ProjectMetadataInstance metadatum in _itemDefinitions [ i ] . Metadata )
1102
+ // Next, any inherited item definitions. Front of the list is highest priority,
1103
+ // so walk backwards.
1104
+ for ( int i = _itemDefinitions . Count - 1 ; i >= 0 ; i -- )
1104
1105
{
1105
- if ( metadatum != null )
1106
- {
1107
- allMetadata . Set ( metadatum ) ;
1108
- }
1109
- else
1106
+ foreach ( ProjectMetadataInstance metadatum in _itemDefinitions [ i ] . Metadata )
1110
1107
{
1111
- Debug . Fail ( $ "metadatum from { _itemDefinitions [ i ] } is null, see https://github.com/dotnet/msbuild/issues/5267") ;
1108
+ if ( metadatum != null )
1109
+ {
1110
+ yield return metadatum ;
1111
+ }
1112
+ else
1113
+ {
1114
+ Debug . Fail ( $ "metadatum from { _itemDefinitions [ i ] } is null, see https://github.com/dotnet/msbuild/issues/5267") ;
1115
+ }
1112
1116
}
1113
1117
}
1114
- }
1115
1118
1116
- // Finally any direct metadata win.
1117
- if ( _directMetadata != null )
1118
- {
1119
- foreach ( ProjectMetadataInstance metadatum in _directMetadata )
1119
+ // Finally any direct metadata win.
1120
+ if ( _directMetadata != null )
1120
1121
{
1121
- if ( metadatum != null )
1122
+ foreach ( ProjectMetadataInstance metadatum in _directMetadata )
1122
1123
{
1123
- allMetadata . Set ( metadatum ) ;
1124
- }
1125
- else
1126
- {
1127
- Debug . Fail ( "metadatum in _directMetadata is null, see https://github.com/dotnet/msbuild/issues/5267" ) ;
1124
+ if ( metadatum != null )
1125
+ {
1126
+ yield return metadatum ;
1127
+ }
1128
+ else
1129
+ {
1130
+ Debug . Fail ( "metadatum in _directMetadata is null, see https://github.com/dotnet/msbuild/issues/5267" ) ;
1131
+ }
1128
1132
}
1129
1133
}
1130
1134
}
1131
-
1132
- return allMetadata ;
1133
1135
}
1134
1136
}
1135
1137
@@ -1694,12 +1696,21 @@ internal void TranslateWithInterning(ITranslator translator, LookasideStringInte
1694
1696
if ( translator . TranslateNullable ( _directMetadata ) )
1695
1697
{
1696
1698
int count = translator . Reader . ReadInt32 ( ) ;
1697
- _directMetadata = ( count == 0 ) ? null : new CopyOnWritePropertyDictionary < ProjectMetadataInstance > ( ) ;
1698
- for ( int i = 0 ; i < count ; i ++ )
1699
+ if ( count > 0 )
1700
+ {
1701
+ IEnumerable < ProjectMetadataInstance > metaData =
1702
+ Enumerable . Range ( 0 , count ) . Select ( _ =>
1703
+ {
1704
+ int key = translator . Reader . ReadInt32 ( ) ;
1705
+ int value = translator . Reader . ReadInt32 ( ) ;
1706
+ return new ProjectMetadataInstance ( interner . GetString ( key ) , interner . GetString ( value ) , allowItemSpecModifiers : true ) ;
1707
+ } ) ;
1708
+ _directMetadata = new CopyOnWritePropertyDictionary < ProjectMetadataInstance > ( ) ;
1709
+ _directMetadata . ImportProperties ( metaData ) ;
1710
+ }
1711
+ else
1699
1712
{
1700
- int key = translator . Reader . ReadInt32 ( ) ;
1701
- int value = translator . Reader . ReadInt32 ( ) ;
1702
- _directMetadata . Set ( new ProjectMetadataInstance ( interner . GetString ( key ) , interner . GetString ( value ) , allowItemSpecModifiers : true ) ) ;
1713
+ _directMetadata = null ;
1703
1714
}
1704
1715
}
1705
1716
}
@@ -1962,10 +1973,8 @@ public void SetMetadata(IEnumerable<Pair<ProjectMetadataElement, string>> metada
1962
1973
{
1963
1974
// Set up a single dictionary that can be applied to all the items
1964
1975
CopyOnWritePropertyDictionary < ProjectMetadataInstance > metadata = new CopyOnWritePropertyDictionary < ProjectMetadataInstance > ( ) ;
1965
- foreach ( Pair < ProjectMetadataElement , string > metadatum in metadataList )
1966
- {
1967
- metadata . Set ( new ProjectMetadataInstance ( metadatum . Key . Name , metadatum . Value ) ) ;
1968
- }
1976
+ IEnumerable < ProjectMetadataInstance > projectMetadataInstances = metadataList . Select ( metadatum => new ProjectMetadataInstance ( metadatum . Key . Name , metadatum . Value ) ) ;
1977
+ metadata . ImportProperties ( projectMetadataInstances ) ;
1969
1978
1970
1979
foreach ( ProjectItemInstance item in destinationItems )
1971
1980
{
0 commit comments