@@ -20,18 +20,18 @@ public sealed class nanoAttributesTable : InanoTable
20
20
/// <summary>
21
21
/// List of custom attributes in Mono.Cecil format for all internal types.
22
22
/// </summary>
23
- private readonly IEnumerable < Tuple < CustomAttribute , ushort > > _typesAttributes ;
23
+ private IEnumerable < Tuple < CustomAttribute , ushort > > _typesAttributes ;
24
24
25
25
/// <summary>
26
26
/// List of custom attributes in Mono.Cecil format for all internal fields.
27
27
/// </summary>
28
28
///
29
- private readonly IEnumerable < Tuple < CustomAttribute , ushort > > _fieldsAttributes ;
29
+ private IEnumerable < Tuple < CustomAttribute , ushort > > _fieldsAttributes ;
30
30
31
31
/// <summary>
32
32
/// List of custom attributes in Mono.Cecil format for all internal methods.
33
33
/// </summary>
34
- private readonly IEnumerable < Tuple < CustomAttribute , ushort > > _methodsAttributes ;
34
+ private IEnumerable < Tuple < CustomAttribute , ushort > > _methodsAttributes ;
35
35
36
36
/// <summary>
37
37
/// Assembly tables context - contains all tables used for building target assembly.
@@ -92,5 +92,51 @@ private void WriteAttributes(
92
92
writer . WriteUInt16 ( _context . SignaturesTable . GetOrCreateSignatureId ( attribute ) ) ;
93
93
}
94
94
}
95
+
96
+ /// <summary>
97
+ /// Remove unused items from attribute tables.
98
+ /// </summary>
99
+ public void RemoveUnusedItems ( HashSet < MetadataToken > set )
100
+ {
101
+ // build a collection of the current items that are present in the used items set
102
+ List < Tuple < CustomAttribute , ushort > > usedItems = new List < Tuple < CustomAttribute , ushort > > ( ) ;
103
+
104
+ // types attributes
105
+ foreach ( var item in _typesAttributes
106
+ . Where ( item => set . Contains ( ( ( IMetadataTokenProvider ) item . Item1 . Constructor ) . MetadataToken ) ) )
107
+ {
108
+ usedItems . Add ( item ) ;
109
+ }
110
+
111
+ // re-create the items dictionary with the used items only
112
+ _typesAttributes = usedItems
113
+ . Select ( a => new Tuple < CustomAttribute , ushort > ( a . Item1 , a . Item2 ) ) ;
114
+
115
+ // fields attributes
116
+ usedItems = new List < Tuple < CustomAttribute , ushort > > ( ) ;
117
+
118
+ foreach ( var item in _fieldsAttributes
119
+ . Where ( item => set . Contains ( ( ( IMetadataTokenProvider ) item . Item1 . Constructor ) . MetadataToken ) ) )
120
+ {
121
+ usedItems . Add ( item ) ;
122
+ }
123
+
124
+ // re-create the items dictionary with the used items only
125
+ _fieldsAttributes = usedItems
126
+ . Select ( a => new Tuple < CustomAttribute , ushort > ( a . Item1 , a . Item2 ) ) ;
127
+
128
+ // methods attributes
129
+ usedItems = new List < Tuple < CustomAttribute , ushort > > ( ) ;
130
+
131
+ foreach ( var item in _methodsAttributes
132
+ . Where ( item => set . Contains ( ( ( IMetadataTokenProvider ) item . Item1 . Constructor ) . MetadataToken ) ) )
133
+ {
134
+ usedItems . Add ( item ) ;
135
+ }
136
+
137
+ // re-create the items dictionary with the used items only
138
+ _methodsAttributes = usedItems
139
+ . Select ( a => new Tuple < CustomAttribute , ushort > ( a . Item1 , a . Item2 ) ) ;
140
+ }
95
141
}
96
142
}
0 commit comments