Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Common/3.3/ObjectExtensions.cs @ 6201

Last change on this file since 6201 was 6201, checked in by mkommend, 13 years ago

#1522: Updated object graph traversing.

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Reflection;
25using System.Collections;
26
27namespace HeuristicLab.Common {
28  public static class ObjectExtensions {
29    public static IEnumerable<object> GetObjectGraphObjects(this object obj) {
30      var objects = new HashSet<object>();
31      obj.CollectObjectGraphObjects(objects);
32      return objects;
33    }
34    /// <summary>
35    /// Types not collected:
36    ///   * System.Delegate
37    ///   * System.EventHandler (+ System.EventHandler<T>)
38    ///   * Primitives (Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single)
39    ///   * string, decimal
40    ///   * Arrays of primitives (+ string, decimal)
41    /// Types of which the fields are not further collected:
42    ///   * System.Type
43    ///   * System.Threading.ThreadLocal<T>
44    /// </summary>
45    private static void CollectObjectGraphObjects(this object obj, HashSet<object> objects) {
46      if (obj == null || objects.Contains(obj)) return;
47      if (obj is Delegate || obj is EventHandler) return;
48      if (obj is Pointer) 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.HasElementType) {
53        Type elementType = type.GetElementType();
54        if (elementType.IsPrimitive || elementType == typeof(string) || elementType == typeof(decimal)) return;
55        //TODO check all types
56      }
57
58      objects.Add(obj);
59
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) {
66        foreach (object value in dictionary.Keys)
67          CollectObjectGraphObjects(value, objects);
68        foreach (object value in dictionary.Values)
69          CollectObjectGraphObjects(value, objects);
70        return;
71      } else if (type.IsArray) {
72        var array = obj as Array;
73        foreach (object value in array)
74          CollectObjectGraphObjects(value, objects);
75        return;
76      }
77
78      foreach (FieldInfo f in type.GetAllFields()) {
79        f.GetValue(obj).CollectObjectGraphObjects(objects);
80      }
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.