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.Data.Linq.Mapping;
\r
29 using System.Reflection;
\r
31 namespace DbLinq.Util
\r
33 internal static class TypeExtensions
\r
36 /// Determines if a given type can have a null value
\r
38 /// <param name="t"></param>
\r
39 /// <returns></returns>
\r
40 public static bool CanBeNull(this Type t)
\r
42 return IsNullable(t) || !t.IsValueType;
\r
46 /// Returns a unique MemberInfo
\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
53 return GetSingleMember(t, name, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
\r
57 /// Returns a unique MemberInfo
\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
65 var members = t.GetMember(name, bindingFlags);
\r
66 if (members.Length > 0)
\r
72 /// Determines if a Type is specified as nullable
\r
74 /// <param name="t"></param>
\r
75 /// <returns></returns>
\r
76 public static bool IsNullable(this Type t)
\r
78 return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>);
\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
85 /// <param name="t"></param>
\r
86 /// <returns></returns>
\r
87 public static Type GetNullableType(this Type t)
\r
89 return Nullable.GetUnderlyingType(t);
\r
93 /// Returns default value for provided type
\r
95 /// <param name="t"></param>
\r
96 /// <returns></returns>
\r
97 public static object GetDefault(this Type t)
\r
99 return TypeConvert.GetDefault(t);
\r
103 /// Returns type name without generic specification
\r
105 /// <param name="t"></param>
\r
106 /// <returns></returns>
\r
107 public static string GetShortName(this Type t)
\r
110 if (t.IsGenericTypeDefinition)
\r
111 return name.Split('`')[0];
\r