Free cookie consent management tool by TermsFeed Policy Generator

source: branches/histogram/HeuristicLab.Common/3.3/ObjectExtensions.cs @ 6219

Last change on this file since 6219 was 6195, checked in by abeham, 14 years ago

#1465

  • updated branch with latest version of trunk
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;
24using System.Collections.Generic;
25using System.Reflection;
26using System.Threading;
27
28namespace HeuristicLab.Common {
29  public static class ObjectExtensions {
30    public static IEnumerable<object> GetObjectGraphObjects(this object obj) {
31      var objects = new HashSet<object>();
32      obj.CollectObjectGraphObjects(objects);
33      return objects;
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>
46    private static void CollectObjectGraphObjects(this object obj, HashSet<object> objects) {
47      if (obj == null || objects.Contains(obj)) return;
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();
54        if (elementType.IsPrimitive || elementType == typeof(string) || elementType == typeof(decimal)) return;
55      }
56
57      objects.Add(obj);
58
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);
67        }
68        foreach (object value in dictionary.Values) {
69          CollectObjectGraphObjects(value, objects);
70        }
71        return;
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      }     
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.