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;
30 using System.Collections.Specialized;
32 using Microsoft.Build.Framework;
34 namespace Microsoft.Build.Utilities
36 public sealed class TaskItem : MarshalByRefObject, ITaskItem
44 this.itemSpec = String.Empty;
45 this.recursiveDir = String.Empty;
46 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
49 public TaskItem (ITaskItem sourceItem)
51 this.itemSpec = sourceItem.ItemSpec;
52 this.recursiveDir = String.Empty;
53 this.metadata = sourceItem.CloneCustomMetadata ();
56 public TaskItem (string itemSpec)
58 this.ItemSpec = itemSpec;
59 this.recursiveDir = String.Empty;
60 this.metadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
63 public TaskItem (string itemSpec, IDictionary itemMetadata)
65 this.itemSpec = itemSpec;
66 this.recursiveDir = String.Empty;
67 this.metadata = itemMetadata;
70 public IDictionary CloneCustomMetadata ()
72 IDictionary clonedMetadata = CollectionsUtil.CreateCaseInsensitiveHashtable ();
73 foreach (DictionaryEntry de in metadata)
74 clonedMetadata.Add (de.Key, de.Value);
75 return clonedMetadata;
78 public void CopyMetadataTo (ITaskItem destinationItem)
80 foreach (DictionaryEntry e in metadata) {
81 destinationItem.SetMetadata ((string)e.Key, (string)e.Value);
85 public static explicit operator string (TaskItem taskItemToCast)
87 return taskItemToCast.ToString ();
90 public string GetMetadata (string metadataName)
92 switch (metadataName.ToLower ()) {
94 return Path.GetFullPath (itemSpec);
98 return Path.GetFileNameWithoutExtension (itemSpec);
100 return Path.GetExtension (itemSpec);
102 return Path.GetDirectoryName (itemSpec);
104 return Path.GetDirectoryName (Path.GetFullPath (itemSpec));
108 return Path.Combine (Path.GetDirectoryName (itemSpec), Path.GetFileName (itemSpec));
110 return File.GetLastWriteTime (itemSpec).ToString ();
112 return File.GetCreationTime (itemSpec).ToString ();
114 return File.GetLastAccessTime (itemSpec).ToString ();
116 return (string) metadata [metadataName];
120 public override object InitializeLifetimeService ()
125 public void RemoveMetadata (string metadataName)
127 if (metadata.Contains (metadataName))
128 metadata.Remove (metadataName);
131 public void SetMetadata (string metadataName, string metadataValue)
133 if (metadata.Contains (metadataName))
134 metadata.Remove (metadataName);
135 metadata.Add (metadataName, metadataValue);
138 public override string ToString ()
143 public string ItemSpec {
144 get { return itemSpec; }
145 set { itemSpec = value; }
148 public int MetadataCount {
149 // predefined metadata
150 get { return metadata.Count + 11; }
153 public ICollection MetadataNames {
154 get { return metadata.Keys; }