private static void InternalSetAttribute(this IAttributeItem item, Attribute value) { int index; var att = GetAttribute(item, value.Key, out index); if (att == null) { // Add attribute if (item.Attributes != null && item.Attributes.Length > 0) { var atts = new Attribute[item.Attributes.Length + 1]; Array.Copy(item.Attributes, atts, item.Attributes.Length); atts[atts.Length - 1] = value; item.Attributes = atts; } else { item.Attributes = new[] { value }; } } else { // Replace attribute var atts = item.Attributes; atts[index] = value; // Hack, damit das Objekt (z.B. SimpleMeasurement) die Änderungen mitbekommt und // evtl. gecachte Daten wieder wegwirft (z.B. SimpleMeasurement._CachedTime) item.Attributes = atts; } }
/// <summary> /// Sets the <code>value</code> for the attribute with the key <code>key</code>. /// </summary> public static void SetAttribute(this IAttributeItem item, Attribute value) { if (value != null) { if (string.IsNullOrEmpty(value.Value)) { RemoveAttribute(item, value.Key); } else { InternalSetAttribute(item, value); } } }
/// <summary> /// Removes the attribute with the key <code>key</code> /// </summary> public static void RemoveAttribute(this IAttributeItem item, ushort key) { int index; var att = GetAttribute(item, key, out index); if (att != null) { // Remove attribute var atts = new Attribute[item.Attributes.Length - 1]; Array.Copy(item.Attributes, 0, atts, 0, index); Array.Copy(item.Attributes, index + 1, atts, index, item.Attributes.Length - index - 1); item.Attributes = atts; } }