// Used to merge two distinct TokenBasedSets (used currently only in PermissionSet Deserialization)
        internal TokenBasedSet SpecialUnion(TokenBasedSet other, ref bool canUnrestrictedOverride)
        {
            // This gets called from PermissionSet.OnDeserialized and it's possible that the TokenBasedSets have
            // not been subjected to VTS callbacks yet
            OnDeserializedInternal();
            TokenBasedSet unionSet = new TokenBasedSet();
            int           maxMax;

            if (other != null)
            {
                other.OnDeserializedInternal();
                maxMax = this.GetMaxUsedIndex() > other.GetMaxUsedIndex() ? this.GetMaxUsedIndex() : other.GetMaxUsedIndex();
            }
            else
            {
                maxMax = this.GetMaxUsedIndex();
            }

            for (int i = 0; i <= maxMax; ++i)
            {
                Object                  thisObj  = this.GetItem(i);
                IPermission             thisPerm = thisObj as IPermission;
                ISecurityElementFactory thisElem = thisObj as ISecurityElementFactory;

                Object                  otherObj  = (other != null)?other.GetItem(i):null;
                IPermission             otherPerm = otherObj as IPermission;
                ISecurityElementFactory otherElem = otherObj as ISecurityElementFactory;

                if (thisObj == null && otherObj == null)
                {
                    continue;
                }


                if (thisObj == null)
                {
                    if (otherElem != null)
                    {
                        otherPerm = PermissionSet.CreatePerm(otherElem, false);
                    }



                    PermissionToken token = PermissionToken.GetToken(otherPerm);

                    if (token == null)
                    {
                        throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
                    }

                    unionSet.SetItem(token.m_index, otherPerm);
                    if (!CodeAccessPermission.CanUnrestrictedOverride(otherPerm))
                    {
                        canUnrestrictedOverride = false;
                    }
                }
                else if (otherObj == null)
                {
                    if (thisElem != null)
                    {
                        thisPerm = PermissionSet.CreatePerm(thisElem, false);
                    }
                    PermissionToken token = PermissionToken.GetToken(thisPerm);
                    if (token == null)
                    {
                        throw new SerializationException(Environment.GetResourceString("Serialization_InsufficientState"));
                    }
                    unionSet.SetItem(token.m_index, thisPerm);
                    if (!CodeAccessPermission.CanUnrestrictedOverride(thisPerm))
                    {
                        canUnrestrictedOverride = false;
                    }
                }
                else
                {
                    BCLDebug.Assert((thisObj == null || otherObj == null), "Permission cannot be in both TokenBasedSets");
                }
            }
            return(unionSet);
        }