/// <summary> /// Private method used by the Set method. This methods sets an attribute /// to a Sequence Attribute and makes sure it contains enough sequence items. /// </summary> /// <param name="tag">The tag of the Attribute.</param> /// <returns>The already existing or newly created sequence item.</returns> private Attribute SetSequenceAttribute(Tag tag) { Attribute attribute = GetAttribute(tag.AsUInt32); if (!(attribute is ValidAttribute)) { attribute = Create(tag.AsUInt32, VR.SQ); } else if (attribute.VR != VR.SQ) { attribute.Delete(); attribute = Create(tag.AsUInt32, VR.SQ); } // At this time, we have made sure that attribute is a Sequence Attribute. // Now take care that it contains enough Sequence Items. for (int sequenceIndex = attribute.ItemCount; sequenceIndex < tag.IndexNumber; sequenceIndex++) { attribute.AddItem(new SequenceItem()); } return(attribute); }
internal virtual void Set <T>(TagSequence tagSequence, VR vR, T genericsParameter) { AttributeSet currentAttributeSetInLoop = this; for (int index = 0; index < tagSequence.Tags.Count; index++) { Tag tag = tagSequence[index]; if (index < tagSequence.Tags.Count - 1) // If this is not the last tag from the TagSequence... { Attribute attribute = currentAttributeSetInLoop.SetSequenceAttribute(tag); currentAttributeSetInLoop = attribute.GetItem(tag.IndexNumber); } else // If this is the last tag from the TagSequence... { Attribute attribute = currentAttributeSetInLoop.GetAttribute(tag.AsUInt32); if (attribute is ValidAttribute) { attribute.Delete(); } attribute = currentAttributeSetInLoop.Create(tag.AsUInt32, vR); // The type of genericsParameter (generics type) is determined run-time. // Maybe this is the reason for a direct call to attribute.Values.Add // (has a overloaded method with params parameter) with the argument // genericsParameter not to work. Needed to explicitly cast to a specfic type // before calling the Add method. if (genericsParameter is Values) { Values values = genericsParameter as Values; attribute.Values.Add(values); } else if (genericsParameter is Byte[]) { Byte[] value = genericsParameter as Byte[]; attribute.Values.Set(value); } else if (genericsParameter is Object[]) { Object[] values = genericsParameter as Object[]; attribute.Values.Add(values); } else { throw new HliException("Not supposed to get here."); } } } }