2 // TaskItem.cs: Represents an item belonging to a task.
5 // Marek Sieradzki (marek.sieradzki@gmail.com)
7 // (C) 2005 Marek Sieradzki
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
31 using Microsoft.Build.Framework;
33 namespace Microsoft.Build.Utilities
35 public sealed class TaskItem : MarshalByRefObject, ITaskItem
43 this.itemSpec = String.Empty;
44 this.recursiveDir = String.Empty;
45 this.metadata = new Hashtable (new CaseInsensitiveHashCodeProvider (), new CaseInsensitiveComparer ());
48 public TaskItem (ITaskItem sourceItem)
50 this.itemSpec = sourceItem.ItemSpec;
51 this.recursiveDir = String.Empty;
52 this.metadata = sourceItem.CloneCustomMetadata ();
55 public TaskItem (string itemSpec)
57 this.ItemSpec = itemSpec;
58 this.recursiveDir = String.Empty;
59 metadata = new Hashtable (new CaseInsensitiveHashCodeProvider (), new CaseInsensitiveComparer ());
62 public TaskItem (string itemSpec, IDictionary itemMetadata)
64 this.itemSpec = itemSpec;
65 this.recursiveDir = String.Empty;
66 this.metadata = itemMetadata;
69 public IDictionary CloneCustomMetadata ()
71 IDictionary clonedMetadata = new Hashtable (new CaseInsensitiveHashCodeProvider (), new CaseInsensitiveComparer ());
72 foreach (DictionaryEntry de in metadata)
73 clonedMetadata.Add (de.Key, de.Value);
74 return clonedMetadata;
77 public void CopyMetadataTo (ITaskItem destinationItem)
79 foreach (DictionaryEntry e in metadata) {
80 destinationItem.SetMetadata ((string)e.Key, (string)e.Value);
84 public static explicit operator string (TaskItem taskItemToCast)
86 return taskItemToCast.ToString ();
89 public string GetMetadata (string metadataName)
91 switch (metadataName.ToLower ()) {
93 return Path.GetFullPath (itemSpec);
97 return Path.GetFileNameWithoutExtension (itemSpec);
99 return Path.GetExtension (itemSpec);
101 return Path.GetDirectoryName (itemSpec);
103 return Path.GetDirectoryName (Path.GetFullPath (itemSpec));
107 return Path.Combine (Path.GetDirectoryName (itemSpec), Path.GetFileName (itemSpec));
109 return File.GetLastWriteTime (itemSpec).ToString ();
111 return File.GetCreationTime (itemSpec).ToString ();
113 return File.GetLastAccessTime (itemSpec).ToString ();
115 return (string) metadata [metadataName];
119 public override object InitializeLifetimeService ()
124 public void RemoveMetadata (string metadataName)
126 if (metadata.Contains (metadataName))
127 metadata.Remove (metadataName);
130 public void SetMetadata (string metadataName, string metadataValue)
132 if (metadata.Contains (metadataName))
133 metadata.Remove (metadataName);
134 metadata.Add (metadataName, metadataValue);
137 public override string ToString ()
142 public string ItemSpec {
143 get { return itemSpec; }
144 set { itemSpec = value; }
147 public int MetadataCount {
148 // predefined metadata
149 get { return metadata.Count + 11; }
152 public ICollection MetadataNames {
153 get { return metadata.Keys; }