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)) {
56 output = o.ToString ();
59 /*if (type == typeof (bool)) {
61 output = t.ToString ();
62 } else if (type == typeof (string)) {
63 string t = (string) o;
64 output = t.ToString ();
65 } else if (type == typeof (DateTime)) {
66 DateTime t = (DateTime) o;
67 output = t.ToString ();
68 } else if (type == typeof (int)) {
70 output = t.ToString ();
71 } else if (type == typeof (uint)) {
73 output = t.ToString ();
78 static string ToString (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 List <string> list = new List <string> ();
104 foreach (object obj in o)
105 list.Add (ToString (obj));
106 return String.Join (";", list.ToArray ());
109 public static BuildProperty ToBuildProperty (object o, Type t, string name)
111 if (t == typeof (ITaskItem)) {
112 return new BuildProperty (name, ((ITaskItem) o).ItemSpec);
113 } else if (t == typeof (ITaskItem [])) {
114 // FIXME move Tostring here
115 return new BuildProperty (name, ToString ((ITaskItem []) o));
116 } else if (t.IsArray) {
117 return new BuildProperty (name, ToString ((object []) o, t));
119 return new BuildProperty (name, ToString (o));
123 public static BuildItemGroup ToBuildItemGroup (object o, Type t, string name)
125 BuildItemGroup big = new BuildItemGroup ();
127 if (t == typeof (ITaskItem)) {
128 big.AddItem (name, (ITaskItem) o);
129 } else if (t == typeof (ITaskItem [])) {
130 foreach (ITaskItem i in (ITaskItem []) o)
131 big.AddItem (name, i);
132 } else if (t.IsArray) {
133 return ToBuildItemGroup (name, ToString ((object []) o, t));
135 return ToBuildItemGroup (name, ToString (o));
141 static BuildItemGroup ToBuildItemGroup (string name, string items)
143 string [] splitItems = items.Split (';');
144 BuildItemGroup big = new BuildItemGroup ();
145 foreach (string item in splitItems)
146 big.AddItem (name, new TaskItem (item));
150 static string ToString (ITaskItem [] items)
152 string [] text = new string [items.Length];
154 foreach (ITaskItem item in items)
155 text [i++] = item.ItemSpec;
156 return String.Join (";", text);