2 // ChangeType.cs: Changes types for output properties or items.
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.
31 using System.Collections;
32 using System.Reflection;
34 using Microsoft.Build.Framework;
35 using Microsoft.Build.Utilities;
37 namespace Microsoft.Build.BuildEngine {
38 internal class ChangeType {
40 static string TemporaryTransform (object o, Type type)
44 if (type == typeof (string))
46 else if (type == typeof (bool) ||
47 type == typeof (int) ||
48 type == typeof (uint) ||
49 type == typeof (float) ||
50 type == typeof (double) ||
51 type == typeof (DateTime)) {
52 output = o.ToString ();
55 /*if (type == typeof (bool)) {
57 output = t.ToString ();
58 } else if (type == typeof (string)) {
59 string t = (string) o;
60 output = t.ToString ();
61 } else if (type == typeof (DateTime)) {
62 DateTime t = (DateTime) o;
63 output = t.ToString ();
64 } else if (type == typeof (int)) {
66 output = t.ToString ();
67 } else if (type == typeof (uint)) {
69 output = t.ToString ();
74 public static string TransformToString (object o, Type type)
76 return TemporaryTransform (o, type);
79 public static string TransformToString (object[] o, Type type)
81 ArrayList al = new ArrayList ();
82 foreach (object obj in o ) {
83 if (type == typeof (bool[])) {
84 al.Add (TemporaryTransform (obj, typeof (bool)));
85 } else if (type == typeof (string[])) {
86 al.Add (TemporaryTransform (obj, typeof (string)));
87 } else if (type == typeof (DateTime[])) {
88 al.Add (TemporaryTransform (obj, typeof (DateTime)));
89 } else if (type == typeof (int[])) {
90 al.Add (TemporaryTransform (obj, typeof (int)));
91 } else if (type == typeof (uint[])) {
92 al.Add (TemporaryTransform (obj, typeof (uint)));
94 throw new Exception (String.Format ("Invalid type: {0}", type.ToString ()));
96 string[] output = new string [al.Count];
98 foreach (string s in al)
100 return String.Join (";", output);
103 public static BuildProperty TransformToBuildProperty (string name, string items)
105 return new BuildProperty (name, items);
108 public static BuildProperty TransformToBuildProperty (string name, ITaskItem[] items)
110 BuildProperty buildProperty;
111 buildProperty = new BuildProperty (name, TransformToString (items));
112 return buildProperty;
115 public static BuildProperty TransformToBuildProperty (string name, ITaskItem item)
117 BuildProperty buildProperty;
118 buildProperty = new BuildProperty (name, TransformToString (item));
119 return buildProperty;
122 public static BuildItemGroup TransformToBuildItemGroup (string name, string items)
124 string[] splittedItems = items.Split (';');
125 BuildItemGroup big = new BuildItemGroup ();
126 foreach (string item in splittedItems)
127 big.AddItem (name, new TaskItem (item));
131 public static BuildItemGroup TransformToBuildItemGroup (string name, ITaskItem[] items)
133 BuildItemGroup big = new BuildItemGroup ();
134 foreach (ITaskItem item in items) {
135 big.AddItem (name, item);
140 public static BuildItemGroup TransformToBuildItemGroup (string name, ITaskItem item)
142 BuildItemGroup big = new BuildItemGroup ();
143 big.AddItem (name, item);
147 static string TransformToString (ITaskItem [] items)
149 string[] text = new string [items.Length];
151 foreach (ITaskItem item in items)
152 text [i++] = item.ItemSpec;
153 return String.Join (";", text);
156 static string TransformToString (ITaskItem item)
158 return item.ItemSpec;