1 | #region License Information
|
---|
2 |
|
---|
3 | /* HeuristicLab
|
---|
4 | * Copyright (C) 2002-2015 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 |
|
---|
24 | using System;
|
---|
25 | using System.Collections.Generic;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Reflection;
|
---|
28 |
|
---|
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();
|
---|
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 IsAssignableTo(this Type subType, Type baseType) {
|
---|
79 | if (baseType.IsAssignableFrom(subType)) return true;
|
---|
80 |
|
---|
81 | //check generics
|
---|
82 | if (!baseType.IsGenericType) return false;
|
---|
83 | if (RecursiveCheckGenericTypes(baseType, subType)) return true;
|
---|
84 |
|
---|
85 | //check generic interfaces
|
---|
86 | IEnumerable<Type> implementedInterfaces = subType.GetInterfaces().Where(t => t.IsGenericType);
|
---|
87 | return implementedInterfaces.Any(implementedInterface => baseType.CheckGenericTypes(implementedInterface));
|
---|
88 | }
|
---|
89 |
|
---|
90 | private static bool RecursiveCheckGenericTypes(Type baseType, Type subType) {
|
---|
91 | if (!baseType.IsGenericType) return false;
|
---|
92 | if (subType.IsGenericType && baseType.CheckGenericTypes(subType)) return true;
|
---|
93 | if (subType.BaseType == null) return false;
|
---|
94 |
|
---|
95 | return RecursiveCheckGenericTypes(baseType, subType.BaseType);
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static bool CheckGenericTypes(this Type baseType, Type subType) {
|
---|
99 | var baseTypeGenericTypeDefinition = baseType.GetGenericTypeDefinition();
|
---|
100 | var subTypeGenericTypeDefinition = subType.GetGenericTypeDefinition();
|
---|
101 | if (baseTypeGenericTypeDefinition != subTypeGenericTypeDefinition) return false;
|
---|
102 |
|
---|
103 | var baseTypeGenericArguments = baseType.GetGenericArguments();
|
---|
104 | var subTypeGenericArguments = subType.GetGenericArguments();
|
---|
105 |
|
---|
106 | for (int i = 0; i < baseTypeGenericArguments.Length; i++) {
|
---|
107 | var baseTypeGenericArgument = baseTypeGenericArguments[i];
|
---|
108 | var subTypeGenericArgument = subTypeGenericArguments[i];
|
---|
109 |
|
---|
110 | //no generic parameters => concrete types => check for type equality (ignore co- and contravariance)
|
---|
111 | //for example List<int> is only a List<int>, IParameter<IItem> is not a base type of IParameter<DoubleValue>
|
---|
112 | if (!baseTypeGenericArgument.IsGenericParameter && !subTypeGenericArgument.IsGenericParameter) {
|
---|
113 | if (baseTypeGenericArgument == subTypeGenericArgument) continue;
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 |
|
---|
117 | //baseTypeGenericArgument is a concrete type and the subTypeGenericArgument is a generic parameter
|
---|
118 | //for example List<int> is not a base type of List<T>
|
---|
119 | if (!baseTypeGenericArgument.IsGenericParameter && subTypeGenericArgument.IsGenericParameter) return false;
|
---|
120 |
|
---|
121 | //baseTypeGenericArugment is a generic parameter and the subTypeGenericArgument is a concrete type => check type constraints
|
---|
122 | //for example IParameter<T> is a base type of IParameter<IItem> if all generic contraints on T are fulfilled
|
---|
123 | if (baseTypeGenericArgument.IsGenericParameter && !subTypeGenericArgument.IsGenericParameter) {
|
---|
124 | if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint) &&
|
---|
125 | subTypeGenericArgument.IsValueType) return false;
|
---|
126 | if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint) &&
|
---|
127 | subTypeGenericArgument.GetConstructor(Type.EmptyTypes) == null) return false;
|
---|
128 | if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) {
|
---|
129 | if (!subTypeGenericArgument.IsValueType) return false;
|
---|
130 | if (subTypeGenericArgument.IsGenericType && subTypeGenericArgument.GetGenericTypeDefinition() == typeof(Nullable<>))
|
---|
131 | return false;
|
---|
132 | }
|
---|
133 |
|
---|
134 | //not assignable if the subTypeGenericArgument is not assignable to all of the constraints of the base type
|
---|
135 | if (baseTypeGenericArgument.GetGenericParameterConstraints().Any(baseTypeGenericParameterConstraint =>
|
---|
136 | !baseTypeGenericParameterConstraint.IsAssignableFrom(subTypeGenericArgument)))
|
---|
137 | return false;
|
---|
138 | }
|
---|
139 |
|
---|
140 | //both generic arguments are generic parameters => check type constraints
|
---|
141 | //for example IParameter<T> is a base type of IParameter<T>
|
---|
142 | if (baseTypeGenericArgument.IsGenericParameter && subTypeGenericArgument.IsGenericParameter) {
|
---|
143 | if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint) &&
|
---|
144 | !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.ReferenceTypeConstraint)) return false;
|
---|
145 | if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint) &&
|
---|
146 | !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.DefaultConstructorConstraint)) return false;
|
---|
147 | if (baseTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint) &&
|
---|
148 | !subTypeGenericArgument.GenericParameterAttributes.HasFlag(GenericParameterAttributes.NotNullableValueTypeConstraint)) return false;
|
---|
149 |
|
---|
150 | //not assignable if any of the constraints is not assignable to the constraints of the base type
|
---|
151 | if (baseTypeGenericArgument.GetGenericParameterConstraints().Any(baseTypeGenericParameterConstraint => !subTypeGenericArgument.GetGenericParameterConstraints().Any(t => baseTypeGenericParameterConstraint.IsAssignableFrom(t)))) {
|
---|
152 | return false;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | return true;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | }
|
---|