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.
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Reflection;
33 using Microsoft.Build.Framework;
34 using Microsoft.Build.Utilities;
36 namespace Microsoft.Build.BuildEngine {
37 internal class ChangeType {
39 // FIXME throw exception here
40 static string ToString (object o)
43 Type type = o.GetType ();
45 // FIXME: there are more types
46 if (type == typeof (string))
48 else if (type == typeof (bool) ||
49 type == typeof (int) ||
50 type == typeof (uint) ||
51 type == typeof (float) ||
52 type == typeof (double) ||
53 type == typeof (DateTime) ||
55 output = o.ToString ();
57 throw new Exception (String.Format ("Unsupported type : {0}", type));
61 static string ToString (object [] o, Type type)
64 ArrayList al = new ArrayList ();
65 foreach (object obj in o ) {
66 if (type == typeof (bool[])) {
67 al.Add (TemporaryTransform (obj, typeof (bool)));
68 } else if (type == typeof (string[])) {
69 al.Add (TemporaryTransform (obj, typeof (string)));
70 } else if (type == typeof (DateTime[])) {
71 al.Add (TemporaryTransform (obj, typeof (DateTime)));
72 } else if (type == typeof (int[])) {
73 al.Add (TemporaryTransform (obj, typeof (int)));
74 } else if (type == typeof (uint[])) {
75 al.Add (TemporaryTransform (obj, typeof (uint)));
77 throw new Exception (String.Format ("Invalid type: {0}", type.ToString ()));
79 string[] output = new string [al.Count];
81 foreach (string s in al)
83 return String.Join (";", output);
86 List <string> list = new List <string> ();
87 foreach (object obj in o)
88 list.Add (ToString (obj));
89 return String.Join (";", list.ToArray ());
92 public static BuildProperty ToBuildProperty (object o, Type t, string name)
94 if (t == typeof (ITaskItem)) {
95 return new BuildProperty (name, ((ITaskItem) o).ItemSpec);
96 } else if (t == typeof (ITaskItem [])) {
97 // FIXME move Tostring here
98 return new BuildProperty (name, ToString ((ITaskItem []) o));
99 } else if (t.IsArray) {
100 return new BuildProperty (name, ToString ((object []) o, t));
102 return new BuildProperty (name, ToString (o));
106 public static BuildItemGroup ToBuildItemGroup (object o, Type t, string name)
108 BuildItemGroup big = new BuildItemGroup ();
110 if (t == typeof (ITaskItem)) {
111 big.AddItem (name, (ITaskItem) o);
112 } else if (t == typeof (ITaskItem [])) {
113 foreach (ITaskItem i in (ITaskItem []) o)
114 big.AddItem (name, i);
115 } else if (t.IsArray) {
116 return ToBuildItemGroup (name, ToString ((object []) o, t), true);
118 return ToBuildItemGroup (name, ToString (o), false);
124 static BuildItemGroup ToBuildItemGroup (string name, string items, bool split)
126 BuildItemGroup big = new BuildItemGroup ();
128 string [] splitItems = items.Split (';');
129 foreach (string item in splitItems)
130 big.AddItem (name, new TaskItem (item));
132 big.AddItem (name, new TaskItem (items));
138 static string ToString (ITaskItem [] items)
140 string [] text = new string [items.Length];
142 foreach (ITaskItem item in items)
143 text [i++] = item.ItemSpec;
144 return String.Join (";", text);