public static AttributeCollection FromExisting(AttributeCollection existing, params Attribute[] newAttributes)
{
if (existing == null)
{
throw new ArgumentNullException("existing");
}
if (newAttributes == null)
{
newAttributes = new Attribute[0];
}
Attribute[] newArray = new Attribute[existing.Count + newAttributes.Length];
int actualCount = existing.Count;
existing.CopyTo(newArray, 0);
for (int idx = 0; idx < newAttributes.Length; idx++)
{
if (newAttributes[idx] == null)
{
throw new ArgumentNullException("newAttributes");
}
// We must see if this attribute is already in the existing
// array. If it is, we replace it.
bool match = false;
for (int existingIdx = 0; existingIdx < existing.Count; existingIdx++)
{
if (newArray[existingIdx].TypeId.Equals(newAttributes[idx].TypeId))
{
match = true;
newArray[existingIdx] = newAttributes[idx];
break;
}
}
if (!match)
{
newArray[actualCount++] = newAttributes[idx];
}
}
// Now, if we collapsed some attributes, create a new array.
//
Attribute[] attributes = null;
if (actualCount < newArray.Length)
{
attributes = new Attribute[actualCount];
Array.Copy(newArray, 0, attributes, 0, actualCount);
}
else
{
attributes = newArray;
}
return new AttributeCollection(attributes);
}