@@ -20,18 +20,18 @@ public sealed class nanoAttributesTable : InanoTable
2020 /// <summary>
2121 /// List of custom attributes in Mono.Cecil format for all internal types.
2222 /// </summary>
23- private readonly IEnumerable < Tuple < CustomAttribute , ushort > > _typesAttributes ;
23+ private IEnumerable < Tuple < CustomAttribute , ushort > > _typesAttributes ;
2424
2525 /// <summary>
2626 /// List of custom attributes in Mono.Cecil format for all internal fields.
2727 /// </summary>
2828 ///
29- private readonly IEnumerable < Tuple < CustomAttribute , ushort > > _fieldsAttributes ;
29+ private IEnumerable < Tuple < CustomAttribute , ushort > > _fieldsAttributes ;
3030
3131 /// <summary>
3232 /// List of custom attributes in Mono.Cecil format for all internal methods.
3333 /// </summary>
34- private readonly IEnumerable < Tuple < CustomAttribute , ushort > > _methodsAttributes ;
34+ private IEnumerable < Tuple < CustomAttribute , ushort > > _methodsAttributes ;
3535
3636 /// <summary>
3737 /// Assembly tables context - contains all tables used for building target assembly.
@@ -92,5 +92,51 @@ private void WriteAttributes(
9292 writer . WriteUInt16 ( _context . SignaturesTable . GetOrCreateSignatureId ( attribute ) ) ;
9393 }
9494 }
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+ }
95141 }
96142}
0 commit comments