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.Collections.Generic;
33 using System.Reflection;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
38 namespace Microsoft.Build.BuildEngine {
39 internal class ChangeType {
41 // FIXME throw exception here
42 static string ToString (object o)
45 Type type = o.GetType ();
47 // FIXME: there are more types
48 if (type == typeof (string))
50 else if (type == typeof (bool) ||
51 type == typeof (int) ||
52 type == typeof (uint) ||
53 type == typeof (float) ||
54 type == typeof (double) ||
55 type == typeof (DateTime) ||
57 output = o.ToString ();
59 throw new Exception (String.Format ("Unsupported type : {0}", type));
63 static string ToString (object [] o, Type type)
66 ArrayList al = new ArrayList ();
67 foreach (object obj in o ) {
68 if (type == typeof (bool[])) {
69 al.Add (TemporaryTransform (obj, typeof (bool)));
70 } else if (type == typeof (string[])) {
71 al.Add (TemporaryTransform (obj, typeof (string)));
72 } else if (type == typeof (DateTime[])) {
73 al.Add (TemporaryTransform (obj, typeof (DateTime)));
74 } else if (type == typeof (int[])) {
75 al.Add (TemporaryTransform (obj, typeof (int)));
76 } else if (type == typeof (uint[])) {
77 al.Add (TemporaryTransform (obj, typeof (uint)));
79 throw new Exception (String.Format ("Invalid type: {0}", type.ToString ()));
81 string[] output = new string [al.Count];
83 foreach (string s in al)
85 return String.Join (";", output);
88 List <string> list = new List <string> ();
89 foreach (object obj in o)
90 list.Add (ToString (obj));
91 return String.Join (";", list.ToArray ());
94 public static BuildProperty ToBuildProperty (object o, Type t, string name)
96 if (t == typeof (ITaskItem)) {
97 return new BuildProperty (name, ((ITaskItem) o).ItemSpec);
98 } else if (t == typeof (ITaskItem [])) {
99 // FIXME move Tostring here
100 return new BuildProperty (name, ToString ((ITaskItem []) o));
101 } else if (t.IsArray) {
102 return new BuildProperty (name, ToString ((object []) o, t));
104 return new BuildProperty (name, ToString (o));
108 public static BuildItemGroup ToBuildItemGroup (object o, Type t, string name)
110 BuildItemGroup big = new BuildItemGroup ();
112 if (t == typeof (ITaskItem)) {
113 big.AddItem (name, (ITaskItem) o);
114 } else if (t == typeof (ITaskItem [])) {
115 foreach (ITaskItem i in (ITaskItem []) o)
116 big.AddItem (name, i);
117 } else if (t.IsArray) {
118 return ToBuildItemGroup (name, ToString ((object []) o, t), true);
120 return ToBuildItemGroup (name, ToString (o), false);
126 static BuildItemGroup ToBuildItemGroup (string name, string items, bool split)
128 BuildItemGroup big = new BuildItemGroup ();
130 string [] splitItems = items.Split (';');
131 foreach (string item in splitItems)
132 big.AddItem (name, new TaskItem (item));
134 big.AddItem (name, new TaskItem (items));
140 static string ToString (ITaskItem [] items)
142 string [] text = new string [items.Length];
144 foreach (ITaskItem item in items)
145 text [i++] = item.ItemSpec;
146 return String.Join (";", text);