- Timestamp:
- 05/17/11 00:09:59 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Common/3.3/ObjectExtensions.cs
r6201 r6205 21 21 22 22 using System; 23 using System.Collections; 23 24 using System.Collections.Generic; 25 using System.Collections.Specialized; 24 26 using System.Reflection; 25 using System. Collections;27 using System.Threading; 26 28 27 29 namespace HeuristicLab.Common { … … 35 37 /// Types not collected: 36 38 /// * System.Delegate 37 /// * System. EventHandler (+ System.EventHandler<T>)39 /// * System.Reflection.Pointer 38 40 /// * Primitives (Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single) 39 41 /// * string, decimal 40 /// * Arrays of primitives (+ string, decimal)41 /// Types of which the fields are not further collected:42 /// * System.Type43 /// * System.Threading.ThreadLocal<T>42 /// * Arrays of types not collected 43 /// 44 /// Dictionaries and HashSets are treated specially, because it is cheaper to iterate over their keys and values 45 /// compared to traverse their internal data structures. 44 46 /// </summary> 45 47 private static void CollectObjectGraphObjects(this object obj, HashSet<object> objects) { 46 48 if (obj == null || objects.Contains(obj)) return; 47 if (obj is Delegate || obj is EventHandler) return;48 if (obj is Pointer) return;49 49 Type type = obj.GetType(); 50 if (type.IsSubclassOfRawGeneric(typeof(EventHandler<>))) return; 51 if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal)) return; 52 if (type.HasElementType) { 53 Type elementType = type.GetElementType(); 54 if (elementType.IsPrimitive || elementType == typeof(string) || elementType == typeof(decimal)) return; 55 //TODO check all types 56 } 50 if (ExcludeType(type)) return; 51 if (type.HasElementType && ExcludeType(type.GetElementType())) return; 57 52 58 53 objects.Add(obj); 59 54 60 //if (typeof(Type).IsInstanceOfType(obj)) return; // avoid infinite recursion 61 //if (type.IsSubclassOfRawGeneric(typeof(ThreadLocal<>))) return; // avoid stack overflow when the field `ConcurrentStack<int> s_availableIndices` grows large 62 63 // performance critical to handle dictionaries in a special way 64 var dictionary = obj as IDictionary; 65 if (dictionary != null) { 55 if (type.IsSubclassOfRawGeneric(typeof(ThreadLocal<>))) return; // avoid stack overflow when the field `ConcurrentStack<int> s_availableIndices` too grows large 56 57 // performance critical to handle dictionaries, hashsets and hashtables in a special way 58 if (type.IsSubclassOfRawGeneric(typeof(Dictionary<,>)) || 59 type.IsSubclassOfRawGeneric(typeof(SortedDictionary<,>)) || 60 type.IsSubclassOfRawGeneric(typeof(SortedList<,>)) || 61 obj is SortedList || 62 obj is OrderedDictionary || 63 obj is ListDictionary || 64 obj is Hashtable) { 65 var dictionary = obj as IDictionary; 66 66 foreach (object value in dictionary.Keys) 67 67 CollectObjectGraphObjects(value, objects); … … 69 69 CollectObjectGraphObjects(value, objects); 70 70 return; 71 } else if (type.IsArray ) {72 var array = obj as Array;73 foreach ( object value in array)71 } else if (type.IsArray || type.IsSubclassOfRawGeneric(typeof(HashSet<>))) { 72 var enumerable = obj as IEnumerable; 73 foreach (var value in enumerable) 74 74 CollectObjectGraphObjects(value, objects); 75 75 return; … … 80 80 } 81 81 } 82 83 private static bool ExcludeType(Type type) { 84 return type.IsPrimitive || 85 type == typeof(string) || 86 type == typeof(decimal) || 87 typeof(Delegate).IsAssignableFrom(type) || 88 typeof(Pointer).IsAssignableFrom(type); 89 } 82 90 } 83 91 }
Note: See TracChangeset
for help on using the changeset viewer.