5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
28 using System.Reflection;
\r
30 namespace DbLinq.Util
\r
33 /// Types conversion.
\r
34 /// A "smart" extension to System.Convert (at least that's what we hope)
\r
36 #if MONO_STRICT || !DEBUG
\r
41 static class TypeConvert
\r
43 public static object ToNumber(object o, Type numberType)
\r
45 if (o.GetType() == numberType)
\r
47 string methodName = string.Format("To{0}", numberType.Name);
\r
48 MethodInfo convertMethod = typeof(Convert).GetMethod(methodName, new[] { o.GetType() });
\r
49 if (convertMethod != null)
\r
50 return convertMethod.Invoke(null, new[] { o });
\r
51 throw new InvalidCastException(string.Format("Can't convert type {0} in Convert.{1}()", o.GetType().Name, methodName));
\r
54 public static U ToNumber<U>(object o)
\r
56 return (U)ToNumber(o, typeof(U));
\r
60 /// Returns the default value for a specified type.
\r
61 /// Reflection equivalent of default(T)
\r
63 /// <param name="t"></param>
\r
64 /// <returns></returns>
\r
65 public static object GetDefault(Type t)
\r
69 return Activator.CreateInstance(t);
\r
73 /// Converts a value to an enum
\r
74 /// (work with literals string values and numbers)
\r
76 /// <param name="o">The literal to convert</param>
\r
77 /// <param name="enumType">The target enum type</param>
\r
78 /// <returns></returns>
\r
79 public static int ToEnum(object o, Type enumType)
\r
81 var e = (int)Enum.Parse(enumType, o.ToString());
\r
85 public static E ToEnum<E>(object o)
\r
87 return (E)(object)ToEnum(o, typeof(E));
\r
90 public static bool ToBoolean(object o)
\r
94 // if it is a string, we may have "T"/"F" or "True"/"False"
\r
100 if (bool.TryParse(lb, out ob))
\r
102 // alternative literals
\r
103 if (lb == "T" || lb == "F")
\r
105 if (lb == "Y" || lb == "N")
\r
108 return ToNumber<int>(o) != 0;
\r
111 public static string ToString(object o)
\r
115 return o.ToString();
\r
118 public static char ToChar(object c)
\r
124 var sc = (string)c;
\r
125 if (sc.Length == 1)
\r
130 throw new InvalidCastException(string.Format("Can't convert type {0} in GetAsChar()", c.GetType().Name));
\r
133 public static Guid ToGuid(object o)
\r
137 return new Guid(ToString(o));
\r
140 public static object To(object o, Type targetType)
\r
142 if (targetType.IsNullable())
\r
146 return Activator.CreateInstance(targetType, To(o, targetType.GetNullableType()));
\r
148 if (targetType == typeof(string))
\r
149 return ToString(o);
\r
150 if (targetType == typeof(bool))
\r
151 return ToBoolean(o);
\r
152 if (targetType == typeof(char))
\r
154 if (targetType == typeof(byte))
\r
155 return ToNumber<byte>(o);
\r
156 if (targetType == typeof(sbyte))
\r
157 return ToNumber<sbyte>(o);
\r
158 if (targetType == typeof(short))
\r
159 return ToNumber<short>(o);
\r
160 if (targetType == typeof(ushort))
\r
161 return ToNumber<ushort>(o);
\r
162 if (targetType == typeof(int))
\r
163 return ToNumber<int>(o);
\r
164 if (targetType == typeof(uint))
\r
165 return ToNumber<uint>(o);
\r
166 if (targetType == typeof(long))
\r
167 return ToNumber<long>(o);
\r
168 if (targetType == typeof(ulong))
\r
169 return ToNumber<ulong>(o);
\r
170 if (targetType == typeof(float))
\r
171 return ToNumber<float>(o);
\r
172 if (targetType == typeof(double))
\r
173 return ToNumber<double>(o);
\r
174 if (targetType == typeof(decimal))
\r
175 return ToNumber<decimal>(o);
\r
176 if (targetType == typeof(DateTime))
\r
177 return (DateTime)o;
\r
178 if (targetType == typeof(Guid))
\r
180 if (targetType.IsEnum)
\r
181 return ToEnum(o, targetType);
\r
182 throw new ArgumentException(string.Format("L0117: Unhandled type {0}", targetType));
\r
185 public static T To<T>(object o)
\r
187 return (T)To(o, typeof(T));
\r