コード例 #1
0
        public void Insert(Int32 indexToInsert, T node)
        {
            if (indexToInsert < 0)
            {
                throw new IndexOutOfRangeException();
            }
            T me = (T)this;

            if (Parent != null && Asn1Reader.GetRestrictedTags().Contains(me.Tag))
            {
                throw new InvalidOperationException();
            }
            node.Parent = this;
            // if indexToInsert is greater than Children length, append node to the end.
            Int32   newOffset;
            Boolean insert;

            if (indexToInsert >= _children.Count)
            {
                newOffset = me.Offset + me.TagLength;
                insert    = false;
            }
            else
            {
                newOffset = _children[indexToInsert].Offset;
                insert    = true;
            }
            Int32 offsetDifference = node.Offset - newOffset;

            // update offsets for inserted node
            updateOffsetByCaller(node, offsetDifference);
            // update offset for all nodes below inserted node
            updateOffsetByIndex(indexToInsert, node.TagLength);
            if (insert)
            {
                _children.Insert(indexToInsert, node);
            }
            else
            {
                _children.Add(node);
            }
            // update binary copy.
            Source.RawData.InsertRange(newOffset, node.GetRawData());
            //
            notifyLengthChanged(node.TagLength);
            Source.RawData.ForceUpdate();
        }