|
8 | 8 | using System;
|
9 | 9 | using System.Collections;
|
10 | 10 | using System.Collections.Generic;
|
| 11 | +using System.Diagnostics; |
11 | 12 | using System.Diagnostics.CodeAnalysis;
|
12 | 13 | using System.Diagnostics.Contracts;
|
13 | 14 | using System.Linq;
|
@@ -42,6 +43,100 @@ public static bool IsDynamicTypeWrapper(this Type type)
|
42 | 43 |
|
43 | 44 | public static bool IsComputeWrapper(this Type type, out Type entityType) => IsTypeWrapper(typeof(ComputeWrapper<>), type, out entityType);
|
44 | 45 |
|
| 46 | + public static bool IsFlatteningWrapper(this Type type) |
| 47 | + { |
| 48 | + if (type == null) |
| 49 | + { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + if (type.IsGenericType) |
| 54 | + { |
| 55 | + Type genericTypeDefinition = type.GetGenericTypeDefinition(); |
| 56 | + |
| 57 | + // Default implementation |
| 58 | + if (genericTypeDefinition == typeof(FlatteningWrapper<>)) |
| 59 | + { |
| 60 | + Debug.Assert( |
| 61 | + typeof(DynamicTypeWrapper).IsAssignableFrom(genericTypeDefinition) |
| 62 | + && genericTypeDefinition.GetInterfaces().Any(d => d.IsGenericType && d.GetGenericTypeDefinition() == typeof(IFlatteningWrapper<>)) |
| 63 | + && genericTypeDefinition.GetInterfaces().Any(d => d.IsGenericType && d.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>)), |
| 64 | + "FlatteningWrapper<T> must inherit from DynamicTypeWrapper and implement IFlatteningWrapper<T> and IGroupByWrapper<T>"); |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + // FlatteningWrapper<T> must inherit from DynamicTypeWrapper |
| 69 | + if (!typeof(DynamicTypeWrapper).IsAssignableFrom(genericTypeDefinition)) |
| 70 | + { |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + // Custom implementation |
| 75 | + Type[] genericTypeInterfaces = genericTypeDefinition.GetInterfaces(); |
| 76 | + bool typeImplementsIFlatteningWrapper = false; |
| 77 | + bool typeImplementsIGroupByWrapper = false; |
| 78 | + for (int i = 0; i < genericTypeInterfaces.Length; i++) |
| 79 | + { |
| 80 | + Type genericTypeInterface = genericTypeInterfaces[i]; |
| 81 | + // FlatteningWrapper<T> must implement IFlatteningWrapper<T> and IGroupByWrapper<T> |
| 82 | + if (genericTypeInterface.IsGenericType && genericTypeInterface.GetGenericTypeDefinition() == typeof(IFlatteningWrapper<>)) |
| 83 | + { |
| 84 | + typeImplementsIFlatteningWrapper = true; |
| 85 | + } |
| 86 | + |
| 87 | + if (genericTypeInterface.IsGenericType && genericTypeInterface.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>)) |
| 88 | + { |
| 89 | + typeImplementsIGroupByWrapper = true; |
| 90 | + } |
| 91 | + |
| 92 | + if (typeImplementsIFlatteningWrapper && typeImplementsIGroupByWrapper) |
| 93 | + { |
| 94 | + return true; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + public static bool IsGroupByWrapper(this Type type) |
| 103 | + { |
| 104 | + if (type == null || type.IsValueType || type == typeof(string)) |
| 105 | + { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + // Default implementation |
| 110 | + if (typeof(GroupByWrapper).IsAssignableFrom(type)) |
| 111 | + { |
| 112 | + Debug.Assert( |
| 113 | + typeof(DynamicTypeWrapper).IsAssignableFrom(type) |
| 114 | + && type.GetInterfaces().Any(d => d.IsGenericType && d.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>)), |
| 115 | + "GroupByWrapper must inherit from DynamicTypeWrapper and implement IGroupByWrapper<T>"); |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + // GroupByWrapper must inherit from DynamicTypeWrapper |
| 120 | + if (!typeof(DynamicTypeWrapper).IsAssignableFrom(type)) |
| 121 | + { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + // Custom implementation |
| 126 | + Type[] typeInterfaces = type.GetInterfaces(); |
| 127 | + for (int i = 0; i < typeInterfaces.Length; i++) |
| 128 | + { |
| 129 | + Type typeInterface = typeInterfaces[i]; |
| 130 | + // GroupByWrapper must implement IGroupByWrapper<T> |
| 131 | + if (typeInterface.IsGenericType && typeInterface.GetGenericTypeDefinition() == typeof(IGroupByWrapper<>)) |
| 132 | + { |
| 133 | + return true; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
45 | 140 | private static bool IsTypeWrapper(Type wrappedType, Type type, out Type entityType)
|
46 | 141 | {
|
47 | 142 | if (type == null)
|
|
0 commit comments