public void CopyMetadataTo(ITaskItem destinationItem) { ErrorUtilities.VerifyThrowArgumentNull(destinationItem, "destinationItem"); string metadata = destinationItem.GetMetadata("OriginalItemSpec"); ITaskItem2 item = destinationItem as ITaskItem2; if (this.metadata != null) { TaskItem item2 = destinationItem as TaskItem; if ((item2 != null) && (item2.metadata == null)) { item2.metadata = this.metadata.Clone(); } else { foreach (KeyValuePair <string, string> pair in this.metadata) { if (item != null) { if (string.IsNullOrEmpty(item.GetMetadataValueEscaped(pair.Key))) { item.SetMetadata(pair.Key, pair.Value); } } else if (string.IsNullOrEmpty(destinationItem.GetMetadata(pair.Key))) { destinationItem.SetMetadata(pair.Key, EscapingUtilities.Escape(pair.Value)); } } } } if (string.IsNullOrEmpty(metadata)) { if (item != null) { item.SetMetadata("OriginalItemSpec", ((ITaskItem2)this).EvaluatedIncludeEscaped); } else { destinationItem.SetMetadata("OriginalItemSpec", EscapingUtilities.Escape(this.ItemSpec)); } } }
/// <summary> /// Copy the metadata (but not the ItemSpec) to destinationItem. If a particular metadata already exists on the /// destination item, then it is not overwritten -- the original value wins. /// </summary> /// <param name="destinationItem">The item to copy metadata to.</param> public void CopyMetadataTo ( ITaskItem destinationItem ) { ErrorUtilities.VerifyThrowArgumentNull(destinationItem, "destinationItem"); // also copy the original item-spec under a "magic" metadata -- this is useful for tasks that forward metadata // between items, and need to know the source item where the metadata came from string originalItemSpec = destinationItem.GetMetadata("OriginalItemSpec"); ITaskItem2 destinationAsITaskItem2 = destinationItem as ITaskItem2; if (_metadata != null) { TaskItem destinationAsTaskItem = destinationItem as TaskItem; // Avoid a copy if we can if (destinationAsTaskItem != null && destinationAsTaskItem.Metadata == null) { destinationAsTaskItem.Metadata = _metadata.Clone(); // Copy on write! } else { foreach (KeyValuePair <string, string> entry in _metadata) { string value; if (destinationAsITaskItem2 != null) { value = destinationAsITaskItem2.GetMetadataValueEscaped(entry.Key); if (String.IsNullOrEmpty(value)) { destinationAsITaskItem2.SetMetadata(entry.Key, entry.Value); } } else { value = destinationItem.GetMetadata(entry.Key); if (String.IsNullOrEmpty(value)) { destinationItem.SetMetadata(entry.Key, EscapingUtilities.Escape(entry.Value)); } } } } } if (String.IsNullOrEmpty(originalItemSpec)) { if (destinationAsITaskItem2 != null) { destinationAsITaskItem2.SetMetadata("OriginalItemSpec", ((ITaskItem2)this).EvaluatedIncludeEscaped); } else { destinationItem.SetMetadata("OriginalItemSpec", EscapingUtilities.Escape(ItemSpec)); } } }