This is a hashtable that stores object keys as weak references. It monitors memory usage and will periodically scavenge the hash table to clean out dead references.
Inheritance: System.Collections.Hashtable
コード例 #1
0
        public static void CreateAssociation(object primary, object secondary)
        {
            if (primary == null)
            {
                throw new ArgumentNullException("primary");
            }
        
            if (secondary == null)
            {
                throw new ArgumentNullException("secondary");
            }

            if (primary == secondary)
            {
                throw new ArgumentException(SR.GetString(SR.TypeDescriptorSameAssociation));
            }

            if (_associationTable == null)
            {
                lock (_internalSyncObject)
                {
                    if (_associationTable == null)
                    {
                        _associationTable = new WeakHashtable();
                    }
                }
            }

            IList associations = (IList)_associationTable[primary];

            if (associations == null)
            {
                lock (_associationTable)
                {
                    associations = (IList)_associationTable[primary];
                    if (associations == null)
                    {
                        associations = new ArrayList(4);
                        _associationTable.SetWeak(primary, associations);
                    }
                }
            }
            else 
            {
                for (int idx = associations.Count - 1; idx >= 0; idx--)
                {
                    WeakReference r = (WeakReference)associations[idx];
                    if (r.IsAlive && r.Target == secondary)
                    {
                        throw new ArgumentException(SR.GetString(SR.TypeDescriptorAlreadyAssociated));
                    }
                }
            }

            lock(associations)
            {
                associations.Add(new WeakReference(secondary));
            }
        }
コード例 #2
0
 public static void CreateAssociation(object primary, object secondary)
 {
     bool flag3;
     IList list2;
     if (primary == null)
     {
         throw new ArgumentNullException("primary");
     }
     if (secondary == null)
     {
         throw new ArgumentNullException("secondary");
     }
     if (primary == secondary)
     {
         throw new ArgumentException(SR.GetString("TypeDescriptorSameAssociation"));
     }
     if (_associationTable == null)
     {
         lock (_internalSyncObject)
         {
             if (_associationTable == null)
             {
                 _associationTable = new WeakHashtable();
             }
         }
     }
     IList list = (IList) _associationTable[primary];
     if (list == null)
     {
         lock (_associationTable)
         {
             list = (IList) _associationTable[primary];
             if (list == null)
             {
                 list = new ArrayList(4);
                 _associationTable.SetWeak(primary, list);
             }
             goto Label_0103;
         }
     }
     for (int i = list.Count - 1; i >= 0; i--)
     {
         WeakReference reference = (WeakReference) list[i];
         if (reference.IsAlive && (reference.Target == secondary))
         {
             throw new ArgumentException(SR.GetString("TypeDescriptorAlreadyAssociated"));
         }
     }
 Label_0103:
     flag3 = false;
     try
     {
         Monitor.Enter(list2 = list, ref flag3);
         list.Add(new WeakReference(secondary));
     }
     finally
     {
         if (flag3)
         {
             Monitor.Exit(list2);
         }
     }
 }