-
Notifications
You must be signed in to change notification settings - Fork 4
/
TypeExtensions.cs
148 lines (126 loc) · 4.93 KB
/
TypeExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// -------------------------------------------------------------------------------------------------------
// LICENSE INFORMATION
//
// - This software is licensed under the MIT shared source license.
// - The "official" source code for this project is maintained at http://oncfext.codeplex.com
//
// Copyright (c) 2010 OpenNETCF Consulting
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// -------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
#if !PCL
namespace System
{
public static class TypeCache
{
private static Dictionary<string, Type> m_cache;
static TypeCache()
{
m_cache = new Dictionary<string, Type>();
}
public static bool TryFindType(string typeName, out Type t)
{
t = null;
lock (m_cache)
{
if (m_cache.ContainsKey(typeName))
{
t = m_cache[typeName];
return true;
}
Assembly[] assemblies;
#if(WindowsCE)
assemblies = new Assembly[] { Assembly.GetExecutingAssembly() };
#else
assemblies = AppDomain.CurrentDomain.GetAssemblies();
#endif
foreach (Assembly a in assemblies)
{
t = a.GetType(typeName);
if (t != null)
{
m_cache.Add(typeName, t);
return true;
}
}
}
return false;
}
}
public static class TypeExtensions
{
private static Dictionary<Type, ConstructorInfo> m_ctorCache = new Dictionary<Type, ConstructorInfo>();
public static bool Implements<TInterface>(this Type baseType)
{
if (!(typeof(TInterface).IsInterface))
{
throw new ArgumentException("TInterface must be an interface type.");
}
return baseType.GetInterfaces().Contains(typeof(TInterface));
}
public static bool Implements(this Type instanceType, Type interfaceType)
{
if (!(interfaceType.IsInterface))
{
throw new ArgumentException("interfaceType must be an interface type.");
}
return instanceType.GetInterfaces().Contains(interfaceType);
}
public static ConstructorInfo GetDefaultConstructor(this Type objectType, bool includePrivateConstructor)
{
if (m_ctorCache.ContainsKey(objectType))
{
return m_ctorCache[objectType];
}
var flags = BindingFlags.Public | BindingFlags.Instance;
if (includePrivateConstructor)
{
flags |= BindingFlags.NonPublic;
}
var ctor = objectType.GetConstructor(flags, null, new Type[0], null);
m_ctorCache.Add(objectType, ctor);
return ctor;
}
public static bool IsNullable(this Type objectType)
{
if (objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return true;
}
return false;
}
public static object[] GetCustomAttributes<T>(this Type type, bool inherit, bool searchInterfaces)
{
if (!searchInterfaces)
{
return type.GetCustomAttributes(inherit);
}
var attributeType = typeof(T);
return type.GetCustomAttributes(attributeType, inherit)
.Union(type.GetInterfaces()
.SelectMany(interfaceType => interfaceType.GetCustomAttributes(attributeType, true)))
.Distinct()
.ToArray();
}
}
}
#endif