#3136
[heuristiclab.git] / HeuristicLab.PluginInfrastructure / 3.3 / TypeExtensions.cs
blob45dc5a9b38c24d0a645176858ec8553236656083
1 #region License Information
3 /* HeuristicLab
4 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
6 * This file is part of HeuristicLab.
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.
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.
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/>.
22 #endregion
24 using System;
25 using System.Collections.Generic;
26 using System.Linq;
27 using System.Reflection;
29 namespace HeuristicLab.PluginInfrastructure {
30 internal static class TypeExtensions {
31 internal static bool IsNonDiscoverableType(this Type t) {
32 return t.GetCustomAttributes(typeof(NonDiscoverableTypeAttribute), false).Any();
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();
45 if (!type.IsGenericTypeDefinition) return type;
46 if (protoType.IsGenericTypeDefinition) return type;
47 if (!protoType.IsGenericType) return type;
49 var typeGenericArguments = type.GetGenericArguments();
50 var protoTypeGenericArguments = protoType.GetGenericArguments();
51 if (typeGenericArguments.Length != protoTypeGenericArguments.Length) return null;
53 for (int i = 0; i < typeGenericArguments.Length; i++) {
54 var typeGenericArgument = typeGenericArguments[i];
55 var protoTypeGenericArgument = protoTypeGenericArguments[i];
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;
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;
65 //check type restrictions on generic type parameter
66 foreach (var constraint in typeGenericArgument.GetGenericParameterConstraints())
67 if (!constraint.IsAssignableFrom(protoTypeGenericArgument)) return null;
70 try {
71 return type.MakeGenericType(protoTypeGenericArguments);
73 catch (Exception) {
74 return null;
78 internal static bool IsSubTypeOf(this Type subType, Type baseType) {
79 if (baseType.IsAssignableFrom(subType)) return true;
80 if (!baseType.IsGenericType) return false;
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;
88 return false;
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;
97 return RecursiveCheckGenericTypes(baseType, subType.BaseType);
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();
107 for (int i = 0; i < baseTypeGenericArguments.Length; i++) {
108 var baseTypeGenericArgument = baseTypeGenericArguments[i];
109 var subTypeGenericArgument = subTypeGenericArguments[i];
111 if (baseTypeGenericArgument.IsGenericParameter ^ subTypeGenericArgument.IsGenericParameter) return false;
112 if (baseTypeGenericArgument == subTypeGenericArgument) continue;
113 if (!baseTypeGenericArgument.IsGenericParameter && !subTypeGenericArgument.IsGenericParameter) return false;
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;
122 foreach (var baseTypeGenericParameterConstraint in baseTypeGenericArgument.GetGenericParameterConstraints()) {
123 if (!subTypeGenericArgument.GetGenericParameterConstraints().Any(t => baseTypeGenericParameterConstraint.IsAssignableFrom(t))) return false;
126 return true;