Exemplo n.º 1
0
        void InitializeNestedClassesDictionary()
        {
            var table     = tablesStream.NestedClassTable;
            var destTable = tablesStream.TypeDefTable;

            Dictionary <uint, bool> validTypeDefRids = null;
            var typeDefRidList = GetTypeDefRidList();

            if ((uint)typeDefRidList.Count != destTable.Rows)
            {
                validTypeDefRids = new Dictionary <uint, bool>(typeDefRidList.Count);
                for (int i = 0; i < typeDefRidList.Count; i++)
                {
                    validTypeDefRids[typeDefRidList[i]] = true;
                }
            }

            var nestedRidsDict = new Dictionary <uint, bool>((int)table.Rows);
            var nestedRids     = new List <uint>((int)table.Rows);              // Need it so we add the rids in correct order

            for (uint rid = 1; rid <= table.Rows; rid++)
            {
                if (validTypeDefRids is not null && !validTypeDefRids.ContainsKey(rid))
                {
                    continue;
                }
                if (!tablesStream.TryReadNestedClassRow(rid, out var row))
                {
                    continue;                           // Should never happen since rid is valid
                }
                if (!destTable.IsValidRID(row.NestedClass) || !destTable.IsValidRID(row.EnclosingClass))
                {
                    continue;
                }
                if (nestedRidsDict.ContainsKey(row.NestedClass))
                {
                    continue;
                }
                nestedRidsDict[row.NestedClass] = true;
                nestedRids.Add(row.NestedClass);
            }

            var newTypeDefRidToNestedClasses = new Dictionary <uint, List <uint> >();
            int count = nestedRids.Count;

            for (int i = 0; i < count; i++)
            {
                var nestedRid = nestedRids[i];
                if (!tablesStream.TryReadNestedClassRow(GetNestedClassRid(nestedRid), out var row))
                {
                    continue;
                }
                if (!newTypeDefRidToNestedClasses.TryGetValue(row.EnclosingClass, out var ridList))
                {
                    newTypeDefRidToNestedClasses[row.EnclosingClass] = ridList = new List <uint>();
                }
                ridList.Add(nestedRid);
            }

            var newNonNestedTypes = new List <uint>((int)(destTable.Rows - nestedRidsDict.Count));

            for (uint rid = 1; rid <= destTable.Rows; rid++)
            {
                if (validTypeDefRids is not null && !validTypeDefRids.ContainsKey(rid))
                {
                    continue;
                }
                if (nestedRidsDict.ContainsKey(rid))
                {
                    continue;
                }
                newNonNestedTypes.Add(rid);
            }

            Interlocked.CompareExchange(ref nonNestedTypes, new StrongBox <RidList>(RidList.Create(newNonNestedTypes)), null);

            // Initialize this one last since it's tested by the callers of this method
            Interlocked.CompareExchange(ref typeDefRidToNestedClasses, newTypeDefRidToNestedClasses, null);
        }