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 private static string TemporaryTransform (object o, Type type)
42 string output = String.Empty;
43 if (type == typeof (bool)) {
45 output = t.ToString ();
46 } else if (type == typeof (string)) {
47 string t = (string) o;
48 output = t.ToString ();
49 } else if (type == typeof (DateTime)) {
50 DateTime t = (DateTime) o;
51 output = t.ToString ();
52 } else if (type == typeof (int)) {
54 output = t.ToString ();
55 } else if (type == typeof (uint)) {
57 output = t.ToString ();
63 public static string TransformToString (object o, Type type)
65 return TemporaryTransform (o, type);
68 public static string TransformToString (object[] o, Type type)
70 ArrayList al = new ArrayList ();
71 foreach (object obj in o ) {
72 if (type == typeof (bool[])) {
73 al.Add (TemporaryTransform (obj, typeof (bool)));
74 } else if (type == typeof (string[])) {
75 al.Add (TemporaryTransform (obj, typeof (string)));
76 } else if (type == typeof (DateTime[])) {
77 al.Add (TemporaryTransform (obj, typeof (DateTime)));
78 } else if (type == typeof (int[])) {
79 al.Add (TemporaryTransform (obj, typeof (int)));
80 } else if (type == typeof (uint[])) {
81 al.Add (TemporaryTransform (obj, typeof (uint)));
83 throw new Exception (String.Format ("Invalid type: {0}", type.ToString ()));
85 string[] output = new string [al.Count];
87 foreach (string s in al)
89 return String.Join (";", output);
92 public static BuildProperty TransformToBuildProperty (string name, string items)
94 return new BuildProperty (name, items);
97 public static BuildProperty TransformToBuildProperty (string name, ITaskItem[] items)
99 BuildProperty buildProperty;
100 buildProperty = new BuildProperty (name, TransformToString (items));
101 return buildProperty;
104 public static BuildProperty TransformToBuildProperty (string name, ITaskItem item)
106 BuildProperty buildProperty;
107 buildProperty = new BuildProperty (name, TransformToString (item));
108 return buildProperty;
111 public static BuildItemGroup TransformToBuildItemGroup (string name, string items)
113 string[] splittedItems = items.Split (';');
114 BuildItemGroup big = new BuildItemGroup ();
115 foreach (string item in splittedItems)
116 big.AddItem (name, new TaskItem (item));
120 public static BuildItemGroup TransformToBuildItemGroup (string name, ITaskItem[] items)
122 BuildItemGroup big = new BuildItemGroup ();
123 foreach (ITaskItem item in items) {
124 big.AddItem (name, item);
129 public static BuildItemGroup TransformToBuildItemGroup (string name, ITaskItem item)
131 BuildItemGroup big = new BuildItemGroup ();
132 big.AddItem (name, item);
136 private static string TransformToString (ITaskItem[] items)
138 string[] text = new string [items.Length];
140 foreach (ITaskItem item in items)
141 text [i++] = item.ItemSpec;
142 return String.Join (";", text);
145 private static string TransformToString (ITaskItem item)
147 return item.ItemSpec;