DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
TypeSystem.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 
20 namespace Deveel.Data.Linq {
21  static class TypeSystem {
22  public static Type GetElementType(Type seqType) {
23  Type ienum = FindIEnumerable(seqType);
24 
25  if (ienum == null)
26  return seqType;
27 
28  return ienum.GetGenericArguments()[0];
29  }
30 
31  private static Type FindIEnumerable(Type seqType) {
32  if (seqType == null || seqType == typeof(string))
33  return null;
34 
35  if (seqType.IsArray)
36  return typeof(IEnumerable<>).MakeGenericType(seqType.GetElementType());
37 
38  if (seqType.IsGenericType) {
39  foreach (Type arg in seqType.GetGenericArguments()) {
40  Type ienum = typeof(IEnumerable<>).MakeGenericType(arg);
41  if (ienum.IsAssignableFrom(seqType)) {
42  return ienum;
43  }
44  }
45  }
46 
47  Type[] ifaces = seqType.GetInterfaces();
48 
49  if (ifaces.Length > 0) {
50  foreach (Type iface in ifaces) {
51  Type ienum = FindIEnumerable(iface);
52  if (ienum != null) return ienum;
53  }
54  }
55 
56  if (seqType.BaseType != null && seqType.BaseType != typeof(object)) {
57  return FindIEnumerable(seqType.BaseType);
58  }
59 
60  return null;
61  }
62  }
63 }
static Type GetElementType(Type seqType)
Definition: TypeSystem.cs:22
static Type FindIEnumerable(Type seqType)
Definition: TypeSystem.cs:31