Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.PluginInfrastructure/3.3/TypeExtensions.cs @ 17369

Last change on this file since 17369 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 6.3 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Collections.Generic;
26using System.Linq;
27using System.Reflection;
28
29namespace HeuristicLab.PluginInfrastructure {
30  internal static class TypeExtensions {
31    internal static bool IsNonDiscoverableType(this Type t) {
32      return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any();
33    }
34
35    /// <summary>
36    /// Constructs a concrete type from a given proto type.
37    /// </summary>
38    /// <param name="type"></param>
39    /// <param name="protoType"></param>
40    /// <returns>The constructed type, a generic type definition or null, if a type construction is not possible</returns>
41    /// <remarks>This method does not work with nested generic types</remarks>
42    internal static Type BuildType(this Type type, Type protoType) {
43      if (type == null || protoType == null) throw new ArgumentNullException();
44
45      if (!type.IsGenericTypeDefinition) return type;
46      if (protoType.IsGenericTypeDefinition) return type;
47      if (!protoType.IsGenericType) return type;
48
49      var typeGenericArguments = type.GetGenericArguments();
50      var protoTypeGenericArguments = protoType.GetGenericArguments();
51      if (typeGenericArguments.Length != protoTypeGenericArguments.Length) return null;
52
53      for (int i = 0; i < typeGenericArguments.Length; i++) {
54        var typeGenericArgument = typeGenericArguments[i];
55        var protoTypeGenericArgument = protoTypeGenericArguments[i];
56
57        //check class contraint on generic type parameter
58        if (typeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint))
59          if (!protoTypeGenericArgument.IsClass && !protoTypeGenericArgument.IsInterface && !protoType.IsArray) return null;
60
61        //check default constructor constraint on generic type parameter
62        if (typeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint))
63          if (!protoTypeGenericArgument.IsValueType && protoTypeGenericArgument.GetConstructor(Type.EmptyTypes) == null) return null;
64
65        //check type restrictions on generic type parameter
66        foreach (var constraint in typeGenericArgument.GetGenericParameterConstraints())
67          if (!constraint.IsAssignableFrom(protoTypeGenericArgument)) return null;
68      }
69
70      try {
71        return type.MakeGenericType(protoTypeGenericArguments);
72      }
73      catch (Exception) {
74        return null;
75      }
76    }
77
78    internal static bool IsSubTypeOf(this Type subType, Type baseType) {
79      if (baseType.IsAssignableFrom(subType)) return true;
80      if (!baseType.IsGenericType) return false;
81
82      if (RecursiveCheckGenericTypes(baseType, subType)) return true;
83      IEnumerable<Type> implementedInterfaces = subType.GetInterfaces().Where(t => t.IsGenericType);
84      foreach (var implementedInterface in implementedInterfaces.Where(i => i.IsGenericType)) {
85        if (baseType.CheckGenericTypes(implementedInterface)) return true;
86      }
87
88      return false;
89    }
90
91    private static bool RecursiveCheckGenericTypes(Type baseType, Type subType) {
92      if (!baseType.IsGenericType) return false;
93      if (!subType.IsGenericType) return false;
94      if (baseType.CheckGenericTypes(subType)) return true;
95      if (subType.BaseType == null) return false;
96
97      return RecursiveCheckGenericTypes(baseType, subType.BaseType);
98    }
99
100    private static bool CheckGenericTypes(this Type baseType, Type subType) {
101      var baseTypeGenericTypeDefinition = baseType.GetGenericTypeDefinition();
102      var subTypeGenericTypeDefinition = subType.GetGenericTypeDefinition();
103      if (baseTypeGenericTypeDefinition != subTypeGenericTypeDefinition) return false;
104      var baseTypeGenericArguments = baseType.GetGenericArguments();
105      var subTypeGenericArguments = subType.GetGenericArguments();
106
107      for (int i = 0; i < baseTypeGenericArguments.Length; i++) {
108        var baseTypeGenericArgument = baseTypeGenericArguments[i];
109        var subTypeGenericArgument = subTypeGenericArguments[i];
110
111        if (baseTypeGenericArgument.IsGenericParameter ^ subTypeGenericArgument.IsGenericParameter) return false;
112        if (baseTypeGenericArgument == subTypeGenericArgument) continue;
113        if (!baseTypeGenericArgument.IsGenericParameter && !subTypeGenericArgument.IsGenericParameter) return false;
114
115        if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint) &&
116            !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint)) return false;
117        if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint) &&
118            !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint)) return false;
119        if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint) &&
120            !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) return false;
121
122        foreach (var baseTypeGenericParameterConstraint in baseTypeGenericArgument.GetGenericParameterConstraints()) {
123          if (!subTypeGenericArgument.GetGenericParameterConstraints().Any(t => baseTypeGenericParameterConstraint.IsAssignableFrom(t))) return false;
124        }
125      }
126      return true;
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.