2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / System.Data.Linq / src / DbLinq / Util / TypeExtensions.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne\r
6 // \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
13 // \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
16 // \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
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 \r
27 using System;\r
28 using System.Data.Linq.Mapping;\r
29 using System.Reflection;\r
30 \r
31 namespace DbLinq.Util\r
32 {\r
33     internal static class TypeExtensions\r
34     {\r
35         /// <summary>\r
36         /// Determines if a given type can have a null value\r
37         /// </summary>\r
38         /// <param name="t"></param>\r
39         /// <returns></returns>\r
40         public static bool CanBeNull(this Type t)\r
41         {\r
42             return IsNullable(t) || !t.IsValueType;\r
43         }\r
44 \r
45         /// <summary>\r
46         /// Returns a unique MemberInfo\r
47         /// </summary>\r
48         /// <param name="t">The declaring type</param>\r
49         /// <param name="name">The member name</param>\r
50         /// <returns>A MemberInfo or null</returns>\r
51         public static MemberInfo GetSingleMember(this Type t, string name)\r
52         {\r
53             return GetSingleMember(t, name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);\r
54         }\r
55 \r
56         /// <summary>\r
57         /// Returns a unique MemberInfo\r
58         /// </summary>\r
59         /// <param name="t">The declaring type</param>\r
60         /// <param name="name">The member name</param>\r
61         /// <param name="bindingFlags">Binding flags</param>\r
62         /// <returns>A MemberInfo or null</returns>\r
63         public static MemberInfo GetSingleMember(this Type t, string name, BindingFlags bindingFlags)\r
64         {\r
65             var members = t.GetMember(name, bindingFlags);\r
66             if (members.Length > 0)\r
67                 return members[0];\r
68             return null;\r
69         }\r
70 \r
71         /// <summary>\r
72         /// Determines if a Type is specified as nullable\r
73         /// </summary>\r
74         /// <param name="t"></param>\r
75         /// <returns></returns>\r
76         public static bool IsNullable(this Type t)\r
77         {\r
78             return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>);\r
79         }\r
80 \r
81         /// <summary>\r
82         /// If the type is nullable, returns the underlying type\r
83         /// Undefined behavior otherwise (it's user responsibility to check for Nullable first)\r
84         /// </summary>\r
85         /// <param name="t"></param>\r
86         /// <returns></returns>\r
87         public static Type GetNullableType(this Type t)\r
88         {\r
89             return Nullable.GetUnderlyingType(t);\r
90         }\r
91 \r
92         /// <summary>\r
93         /// Returns default value for provided type\r
94         /// </summary>\r
95         /// <param name="t"></param>\r
96         /// <returns></returns>\r
97         public static object GetDefault(this Type t)\r
98         {\r
99             return TypeConvert.GetDefault(t);\r
100         }\r
101 \r
102         /// <summary>\r
103         /// Returns type name without generic specification\r
104         /// </summary>\r
105         /// <param name="t"></param>\r
106         /// <returns></returns>\r
107         public static string GetShortName(this Type t)\r
108         {\r
109             var name = t.Name;\r
110             if (t.IsGenericTypeDefinition)\r
111                 return name.Split('`')[0];\r
112             return name;\r
113         }\r
114     }\r
115 }\r