forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeSystem.cs
88 lines (81 loc) · 2.28 KB
/
TypeSystem.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
//-----------------------------------------------------------------------
// <copyright file="TypeSystem.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Raven.Client.Linq
{
internal static class TypeSystem
{
private static Type FindIEnumerable(Type seqType)
{
if (seqType == null || seqType == typeof(string))
return null;
if (seqType.IsArray)
return typeof(IEnumerable<>).MakeGenericType(seqType.GetElementType());
if (seqType.IsGenericType)
{
foreach (Type arg in seqType.GetGenericArguments())
{
Type ienum = typeof(IEnumerable<>).MakeGenericType(arg);
if (ienum.IsAssignableFrom(seqType))
return ienum;
}
}
Type[] ifaces = seqType.GetInterfaces();
if (ifaces != null && ifaces.Length > 0)
{
foreach (Type iface in ifaces)
{
Type ienum = FindIEnumerable(iface);
if (ienum != null)
return ienum;
}
}
if (seqType.BaseType != null && seqType.BaseType != typeof(object))
return FindIEnumerable(seqType.BaseType);
return null;
}
internal static Type GetSequenceType(Type elementType)
{
return typeof(IEnumerable<>).MakeGenericType(elementType);
}
internal static Type GetElementType(Type seqType)
{
Type ienum = FindIEnumerable(seqType);
if (ienum == null)
return seqType;
return ienum.GetGenericArguments()[0];
}
internal static bool IsNullableType(Type type)
{
return type != null && type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
internal static bool IsNullAssignable(Type type)
{
return !type.IsValueType || IsNullableType(type);
}
internal static Type GetNonNullableType(Type type)
{
if (IsNullableType(type))
return type.GetGenericArguments()[0];
return type;
}
internal static Type GetMemberType(MemberInfo mi)
{
FieldInfo fi = mi as FieldInfo;
if (fi != null)
return fi.FieldType;
PropertyInfo pi = mi as PropertyInfo;
if (pi != null)
return pi.PropertyType;
EventInfo ei = mi as EventInfo;
if (ei != null)
return ei.EventHandlerType;
return null;
}
}
}