Changeset 2724 for trunk/sources/HeuristicLab.MainForm
- Timestamp:
- 01/29/10 16:27:44 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.MainForm/3.2
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.MainForm/3.2/ContentAttribute.cs
r2696 r2724 60 60 select a.type; 61 61 } 62 63 internal static IEnumerable<Type> GetViewableTypes(Type viewType) { 64 ContentAttribute[] attributes = (ContentAttribute[])viewType.GetCustomAttributes(typeof(ContentAttribute), false); 65 return from a in attributes 66 select a.type; 67 } 62 68 } 63 69 } -
trunk/sources/HeuristicLab.MainForm/3.2/MainFormManager.cs
r2702 r2724 76 76 77 77 public static IEnumerable<Type> GetViewTypes(Type contentType) { 78 return from v in views 79 where ContentAttribute.CanViewType(v, contentType) 80 select v; 78 List<Type> viewTypes = (from v in views 79 where ContentAttribute.CanViewType(v, contentType) 80 select v).ToList(); 81 //transform generic type definitions to generic types 82 for (int i = 0; i < viewTypes.Count; i++) { 83 viewTypes[i] = TransformGenericTypeDefinition(viewTypes[i], contentType); 84 } 85 return viewTypes; 81 86 } 82 87 … … 93 98 Type type = contentType; 94 99 while (type != null) { 95 foreach (Type default ViewType in defaultViews.Keys) {96 if (type == default ViewType || type.CheckGenericTypes(defaultViewType))97 return defaultViews[defaultViewType];100 foreach (Type defaultContentType in defaultViews.Keys) { 101 if (type == defaultContentType || type.CheckGenericTypes(defaultContentType)) 102 return TransformGenericTypeDefinition(defaultViews[defaultContentType], contentType); 98 103 } 99 104 type = type.BaseType; … … 105 110 select t).ToList(); 106 111 if (temp.Count == 1) 107 return defaultViews[temp[0]];112 return TransformGenericTypeDefinition(defaultViews[temp[0]], contentType); 108 113 //more than one default view for implemented interfaces are found 109 114 if (temp.Count > 1) … … 117 122 return null; 118 123 119 Type viewType = TransformGenericTypeDefinition(t, objectToView); 120 if (viewType == null) 121 return null; 122 123 return (IView)Activator.CreateInstance(viewType, objectToView); 124 return (IView)Activator.CreateInstance(t, objectToView); 124 125 } 125 126 … … 136 137 if (!typeof(IView).IsAssignableFrom(viewType)) 137 138 throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is not of type IView."); 139 if (viewType.IsGenericTypeDefinition) 140 throw new ArgumentException("View can not be created becaues given type " + viewType.ToString() + " is a generic type definition."); 138 141 139 Type t = TransformGenericTypeDefinition(viewType, objectToView); 140 if (t == null) 141 return null; 142 143 return (IView)Activator.CreateInstance(t, objectToView); 142 return (IView)Activator.CreateInstance(viewType, objectToView); 144 143 } 145 144 146 private static Type TransformGenericTypeDefinition(Type type, object objectToView) {147 if ( !type.IsGenericTypeDefinition)148 return type;145 private static Type TransformGenericTypeDefinition(Type viewType, Type contentType) { 146 if (contentType.IsGenericTypeDefinition) 147 throw new ArgumentException("The content type " + contentType.ToString() + " must not be a generic type definition."); 149 148 150 Type[] typeGenericArguments = type.GetGenericArguments();151 Type[] objectGenericArguments = objectToView.GetType().GetGenericArguments();149 if (!viewType.IsGenericTypeDefinition) 150 return viewType; 152 151 153 for (int i = 0; i < typeGenericArguments.Length; i++) { 154 foreach (Type typeConstraint in typeGenericArguments[i].GetGenericParameterConstraints()) { 155 if (!typeConstraint.IsAssignableFrom(objectGenericArguments[i])) 152 Type contentTypeBaseType = contentType; 153 foreach (Type type in ContentAttribute.GetViewableTypes(viewType)) { 154 while (contentTypeBaseType != null && (!contentTypeBaseType.IsGenericType || 155 type.GetGenericTypeDefinition() != contentTypeBaseType.GetGenericTypeDefinition())) 156 contentTypeBaseType = contentTypeBaseType.BaseType; 157 158 //check interfaces for generic type arguments 159 if (contentTypeBaseType == null) { 160 IEnumerable<Type> implementedInterfaces = contentType.GetInterfaces().Where(t => t.IsGenericType); 161 foreach (Type implementedInterface in implementedInterfaces) { 162 if (implementedInterface.CheckGenericTypes(viewType)) 163 contentTypeBaseType = implementedInterface; 164 } 165 } 166 } 167 168 if (!contentTypeBaseType.IsGenericType) 169 throw new ArgumentException("Neither content type itself nor any of its base classes is a generic type. Could not determine generic type argument for the view."); 170 171 Type[] viewTypeGenericArguments = viewType.GetGenericArguments(); 172 Type[] contentTypeGenericArguments = contentTypeBaseType.GetGenericArguments(); 173 174 if (contentTypeGenericArguments.Length != viewTypeGenericArguments.Length) 175 throw new ArgumentException("Neiter the type (" + contentType.ToString() + ") nor any of its base types specifies " + 176 viewTypeGenericArguments.Length + " generic type arguments."); 177 178 for (int i = 0; i < viewTypeGenericArguments.Length; i++) { 179 foreach (Type typeConstraint in viewTypeGenericArguments[i].GetGenericParameterConstraints()) { 180 if (!typeConstraint.IsAssignableFrom(contentTypeGenericArguments[i])) 156 181 return null; 157 182 } 158 183 } 159 184 160 Type t = type.MakeGenericType(objectToView.GetType().GetGenericArguments());161 return t;185 Type returnType = viewType.MakeGenericType(contentTypeGenericArguments); 186 return returnType; 162 187 } 163 188 } -
trunk/sources/HeuristicLab.MainForm/3.2/TypeExtension.cs
r2696 r2724 32 32 return true; 33 33 34 if (type.IsGenericType && other.IsGenericType) { 35 if (recursiveCheckGenericTypes(type, other)) 34 if (recursiveCheckGenericTypes(type, other)) 35 return true; 36 37 IEnumerable<Type> implementedInterfaces = type.GetInterfaces().Where(t => t.IsGenericType); 38 foreach (Type implementedInterface in implementedInterfaces) { 39 if (implementedInterface.CheckGenericTypes(other)) 36 40 return true; 41 } 37 42 38 IEnumerable<Type> implementedInterfaces = type.GetInterfaces().Where(t => t.IsGenericType);39 foreach (Type implementedInterface in implementedInterfaces) {40 if (implementedInterface.CheckGenericTypes(other))41 return true;42 }43 }44 43 return false; 45 44 } … … 79 78 return true; 80 79 } 80 81 internal static Type[] ExtractGenericTypeArguments(this Type type, Type other) { 82 Type[] types = new Type[0]; 83 return types; 84 } 81 85 } 82 86 }
Note: See TracChangeset
for help on using the changeset viewer.