- Timestamp:
- 05/14/11 10:40:50 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Common/3.3/ObjectExtensions.cs
r6181 r6192 24 24 using System.Collections.Generic; 25 25 using System.Reflection; 26 using System.Threading; 26 27 27 28 namespace HeuristicLab.Common { … … 32 33 return objects; 33 34 } 34 35 /// <summary> 36 /// Types not collected: 37 /// * System.Delegate 38 /// * System.EventHandler (+ System.EventHandler<T>) 39 /// * Primitives (Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single) 40 /// * string, decimal 41 /// * Arrays of primitives (+ string, decimal) 42 /// Types of which the fields are not further collected: 43 /// * System.Type 44 /// * System.Threading.ThreadLocal<T> 45 /// </summary> 35 46 private static void CollectObjectGraphObjects(this object obj, HashSet<object> objects) { 36 47 if (obj == null || objects.Contains(obj)) return; 37 if (obj is ValueType || obj is string) return; 38 if (obj is Delegate || obj is EventHandler) return; 39 if (obj.GetType().IsSubclassOfRawGeneric(typeof(EventHandler<>))) return; 40 if (obj.GetType().GetElementType() != null) { 41 Type elementType = obj.GetType().GetElementType(); 48 if (obj is Delegate || obj is EventHandler) return; 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.IsArray) { 53 Type elementType = type.GetElementType(); 42 54 if (elementType.IsPrimitive || elementType == typeof(string) || elementType == typeof(decimal)) return; 43 55 } … … 45 57 objects.Add(obj); 46 58 47 IEnumerable enumerable = obj as IEnumerable; 48 if (enumerable != null) { 49 foreach (object value in enumerable) { 50 value.CollectObjectGraphObjects(objects); 59 if (typeof(Type).IsInstanceOfType(obj)) return; // avoid infinite recursion 60 if (type.IsSubclassOfRawGeneric(typeof(ThreadLocal<>))) return; // avoid stack overflow when the field `ConcurrentStack<int> s_availableIndices` grows large 61 62 // performance critical to handle dictionaries in a special way 63 var dictionary = obj as IDictionary; 64 if (dictionary != null) { 65 foreach (object value in dictionary.Keys) { 66 CollectObjectGraphObjects(value, objects); 51 67 } 52 } else { 53 foreach (FieldInfo f in obj.GetType().GetAllFields()) { 54 f.GetValue(obj).CollectObjectGraphObjects(objects); 68 foreach (object value in dictionary.Values) { 69 CollectObjectGraphObjects(value, objects); 55 70 } 71 return; 56 72 } 73 74 if (type.IsArray) { 75 var array = obj as Array; 76 foreach (object value in array) { 77 CollectObjectGraphObjects(value, objects); 78 } 79 return; 80 } 81 82 foreach (FieldInfo f in type.GetAllFields()) { 83 f.GetValue(obj).CollectObjectGraphObjects(objects); 84 } 57 85 } 58 86 }
Note: See TracChangeset
for help on using the changeset viewer.