using System;
using System.Collections;
+using System.Collections.Generic;
using System.Reflection;
+using System.Xml;
namespace Microsoft.Build.BuildEngine {
public class TargetCollection : ICollection, IEnumerable {
- IDictionary targetsByName;
- Project parentProject;
+ Dictionary <string, Target> targetsByName;
+ Project parentProject;
internal TargetCollection (Project project)
{
- this.targetsByName = new Hashtable ();
+ this.targetsByName = new Dictionary <string, Target> (StringComparer.InvariantCultureIgnoreCase);
this.parentProject = project;
}
- // This must create a new xml element and stuff.
[MonoTODO]
public Target AddNewTarget (string targetName)
{
- throw new NotImplementedException ();
+ if (targetName == null)
+ throw new InvalidProjectFileException (
+ "The required attribute \"Name\" is missing from element <Target>.");
+
+ XmlElement targetElement = parentProject.XmlDocument.CreateElement ("Target", Project.XmlNamespace);
+ parentProject.XmlDocument.DocumentElement.AppendChild (targetElement);
+ targetElement.SetAttribute ("Name", targetName);
+
+ Target t = new Target (targetElement, parentProject, null);
+
+ AddTarget (t);
+
+ return t;
}
internal void AddTarget (Target target)
{
+ if (targetsByName.ContainsKey (target.Name))
+ targetsByName.Remove (target.Name);
targetsByName.Add (target.Name, target);
}
public void CopyTo (Array array, int index)
{
- targetsByName.Values.CopyTo (array, index);
+ targetsByName.Values.CopyTo ((Target[]) array, index);
}
public bool Exists (string targetName)
{
- return targetsByName.Contains (targetName);
+ return targetsByName.ContainsKey (targetName);
}
public IEnumerator GetEnumerator ()
{
- foreach (DictionaryEntry de in targetsByName) {
- yield return (Target)de.Key;
- }
+ foreach (KeyValuePair <string, Target> kvp in targetsByName)
+ yield return kvp.Value;
}
public void RemoveTarget (Target targetToRemove)
{
+ if (targetToRemove == null)
+ throw new ArgumentNullException ();
+
targetsByName.Remove (targetToRemove.Name);
}
}
}
- public Target this[string index] {
+ public Target this [string index] {
get {
- return (Target) targetsByName [index];
+ if (targetsByName.ContainsKey (index))
+ return targetsByName [index];
+ else
+ return null;
}
}
}