Changeset 4810 for branches/HeuristicLab.Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.JobBase/3.3/PluginUtil.cs
- Timestamp:
- 11/16/10 14:31:52 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.JobBase/3.3/PluginUtil.cs
r4424 r4810 24 24 using System.Reflection; 25 25 using HeuristicLab.PluginInfrastructure; 26 using System.Linq; 27 using HeuristicLab.Common; 26 28 27 29 namespace HeuristicLab.Hive.JobBase { 28 30 public static class PluginUtil { 29 31 #region Required Plugin Search 30 32 31 33 /// <summary> 32 34 /// Returns a list of plugins in which the type itself and all members 33 35 /// of the type are declared. Objectgraph is searched recursively. 34 36 /// </summary> 35 public static IEnumerable<IPluginDescription> GetDeclaringPlugins(Type type) { 37 public static IEnumerable<IPluginDescription> GetDeclaringPlugins(IDeepCloneable obj) { 38 object clone = obj.Clone(); 36 39 HashSet<Type> types = new HashSet<Type>(); 37 FindTypes(type, types, "HeuristicLab."); 38 return GetDeclaringPlugins(types); 40 HashSet<object> objects = new HashSet<object>(); 41 string namespaceStart = "HeuristicLab."; 42 FindTypesInObject(clone, objects, types, namespaceStart, 10); 43 return GetDeclaringPlugins(types, namespaceStart); 39 44 } 40 45 … … 42 47 /// Returns the plugins (including dependencies) in which the given types are declared 43 48 /// </summary> 44 public static IEnumerable<IPluginDescription> GetDeclaringPlugins(IEnumerable<Type> types ) {49 public static IEnumerable<IPluginDescription> GetDeclaringPlugins(IEnumerable<Type> types, string namespaceStart) { 45 50 HashSet<IPluginDescription> plugins = new HashSet<IPluginDescription>(); 46 foreach (Type t in types) { 51 var hlTypes = types.Where(x => x.Namespace != null && x.Namespace.StartsWith(namespaceStart)); 52 foreach (Type t in hlTypes) { 47 53 FindDeclaringPlugins(ApplicationManager.Manager.GetDeclaringPlugin(t), plugins); 48 54 } … … 68 74 /// </summary> 69 75 /// <param name="type">the type to be searched</param> 70 /// <param name="types">found types will be stored there, needed in order to avoid duplicates</param> 71 /// <param name="namespaceStart">only types from namespaces which start with this will be searched and added</param> 72 public static void FindTypes(Type type, HashSet<Type> types, string namespaceStart) { 73 if (!types.Contains(type) && type.Namespace.StartsWith(namespaceStart)) { 74 types.Add(type); 76 /// <param name="foundTypes">found types will be stored there, needed in order to avoid duplicates</param> 77 public static void FindTypes(Type type, HashSet<Type> foundTypes, string namespaceStart, int nonNamespaceSearchDepth) { 78 if (type.Namespace != null && type.Namespace.StartsWith(namespaceStart)) { 79 nonNamespaceSearchDepth = 10; 80 } else { 81 nonNamespaceSearchDepth--; 82 } 83 84 if (!foundTypes.Contains(type) && nonNamespaceSearchDepth > 0) { 85 foundTypes.Add(type); 86 87 if (type.IsGenericType) { 88 foreach (Type t in type.GetGenericArguments()) { 89 FindTypes(t, foundTypes, namespaceStart, nonNamespaceSearchDepth); 90 } 91 } 75 92 76 93 // constructors 77 94 foreach (ConstructorInfo info in type.GetConstructors()) { 78 95 foreach (ParameterInfo paramInfo in info.GetParameters()) { 79 FindTypes(paramInfo.ParameterType, types, namespaceStart);96 FindTypes(paramInfo.ParameterType, foundTypes, namespaceStart, nonNamespaceSearchDepth); 80 97 } 81 98 } … … 83 100 // interfaces 84 101 foreach (Type t in type.GetInterfaces()) { 85 FindTypes(t, types, namespaceStart);102 FindTypes(t, foundTypes, namespaceStart, nonNamespaceSearchDepth); 86 103 } 87 104 88 105 // events 89 106 foreach (EventInfo info in type.GetEvents()) { 90 FindTypes(info.EventHandlerType, types, namespaceStart); 91 FindTypes(info.DeclaringType, types, namespaceStart); 92 } 93 94 // properties 95 foreach (PropertyInfo info in type.GetProperties()) { 96 FindTypes(info.PropertyType, types, namespaceStart); 97 } 98 99 // fields 100 foreach (FieldInfo info in type.GetFields()) { 101 FindTypes(info.FieldType, types, namespaceStart); 107 FindTypes(info.EventHandlerType, foundTypes, namespaceStart, nonNamespaceSearchDepth); 108 FindTypes(info.DeclaringType, foundTypes, namespaceStart, nonNamespaceSearchDepth); 102 109 } 103 110 … … 105 112 foreach (MethodInfo info in type.GetMethods()) { 106 113 foreach (ParameterInfo paramInfo in info.GetParameters()) { 107 FindTypes(paramInfo.ParameterType, types, namespaceStart);114 FindTypes(paramInfo.ParameterType, foundTypes, namespaceStart, nonNamespaceSearchDepth); 108 115 } 109 FindTypes(info.ReturnType, types, namespaceStart); 116 FindTypes(info.ReturnType, foundTypes, namespaceStart, nonNamespaceSearchDepth); 117 } 118 119 // base type 120 if (type.BaseType != null) { 121 FindTypes(type.BaseType, foundTypes, namespaceStart, nonNamespaceSearchDepth); 110 122 } 111 123 } 124 } 125 126 public static void FindTypesInObject(object obj, HashSet<object> visitedObjects, HashSet<Type> types, string namespaceStart, int nonNamespaceSearchDepth) { 127 Type type = obj.GetType(); 128 FindTypes(type, types, namespaceStart, nonNamespaceSearchDepth); 129 130 if (type.Namespace != null && type.Namespace.StartsWith(namespaceStart)) { 131 nonNamespaceSearchDepth = 10; 132 } else { 133 nonNamespaceSearchDepth--; 134 } 135 136 if (!visitedObjects.Contains(obj) && nonNamespaceSearchDepth > 0) { 137 visitedObjects.Add(obj); 138 139 // fields 140 foreach (FieldInfo info in GetAllFields(type)) { 141 var value = info.GetValue(obj); 142 if (value == null) { 143 FindTypes(info.FieldType, types, namespaceStart, nonNamespaceSearchDepth); 144 } else { 145 FindTypesInObject(value, visitedObjects, types, namespaceStart, nonNamespaceSearchDepth); 146 } 147 } 148 } 149 } 150 151 public static IEnumerable<FieldInfo> GetAllFields(Type t) { 152 BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy; 153 return t.GetFields(flags); 112 154 } 113 155 #endregion
Note: See TracChangeset
for help on using the changeset viewer.