Changeset 8310


Ignore:
Timestamp:
07/20/12 08:57:40 (7 years ago)
Author:
mkommend
Message:

#1898:

  • Corrected GetAllFields extension methods to return fields accross the hierarchy only once.
  • Changed filtering of object and types to first check for excluded types.
  • Removed unnecassary comparer for excluded members HashSet.
  • Corrected outdated comments
  • Added SignatureHelper to the excluded types.
Location:
trunk/sources/HeuristicLab.Common/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Common/3.3/ObjectExtensions.cs

    r8220 r8310  
    4848
    4949        foreach (object o in GetChildObjects(current, excludedMembers, excludeStaticMembers)) {
    50           if (o != null && !objects.Contains(o) && !ExcludeType(o.GetType()))
    51             stack.Push(o);
     50          if (o == null) continue;
     51          if (ExcludeType(o.GetType())) continue;
     52          if (objects.Contains(o)) continue;
     53          stack.Push(o);
    5254        }
    5355      }
     
    6365    ///   * string, decimal, DateTime
    6466    ///   * Arrays of types not collected
    65     ///   
    66     /// Dictionaries and HashSets are treated specially, because it is cheaper to iterate over their keys and values
    67     /// compared to traverse their internal data structures.
    6867    /// </summary>
    6968    private static bool ExcludeType(Type type) {
    7069      return type.IsPrimitive ||
    7170             type == typeof(string) ||
     71             type == typeof(string[]) ||
    7272             type == typeof(decimal) ||
     73             type == typeof(decimal[]) ||
    7374             type == typeof(DateTime) ||
     75             type == typeof(DateTime[]) ||
    7476             typeof(Delegate).IsAssignableFrom(type) ||
    7577             typeof(Pointer).IsAssignableFrom(type) ||
    76              type == typeof(string[]) ||
    77              type == typeof(DateTime[]) ||
     78             type == typeof(System.Reflection.Emit.SignatureHelper) ||
    7879             (type.HasElementType && ExcludeType(type.GetElementType()));
    7980    }
    8081
    81     private static ReferenceEqualityComparer comparer = new ReferenceEqualityComparer();
    8282    private static IEnumerable<object> GetChildObjects(object obj, HashSet<object> excludedMembers, bool excludeStaticMembers) {
    8383      Type type = obj.GetType();
     
    8686        PropertyInfo info = type.GetProperty("Value");
    8787        object value = info.GetValue(obj, null);
    88         if (value != null && !excludedMembers.Contains(value, comparer))
     88        if (value != null && !excludedMembers.Contains(value))
    8989          yield return value;
    9090      } else if (type.IsSubclassOfRawGeneric(typeof(Dictionary<,>)) ||
     
    9797        var dictionary = obj as IDictionary;
    9898        foreach (object value in dictionary.Keys) {
    99           if (excludedMembers.Contains(value, comparer)) continue;
     99          if (excludedMembers.Contains(value)) continue;
    100100          yield return value;
    101101        }
    102102        foreach (object value in dictionary.Values) {
    103           if (excludedMembers.Contains(value, comparer)) continue;
     103          if (excludedMembers.Contains(value)) continue;
    104104          yield return value;
    105105        }
     
    107107        var enumerable = obj as IEnumerable;
    108108        foreach (var value in enumerable) {
    109           if (excludedMembers.Contains(value, comparer)) continue;
     109          if (excludedMembers.Contains(value)) continue;
    110110          yield return value;
    111111        }
     
    116116          try {
    117117            fieldValue = f.GetValue(obj);
    118           } catch (SecurityException) {
     118          }
     119          catch (SecurityException) {
    119120            continue;
    120121          }
    121           if (excludedMembers.Contains(fieldValue, comparer)) continue;
     122          if (excludedMembers.Contains(fieldValue)) continue;
    122123          yield return fieldValue;
    123124        }
  • trunk/sources/HeuristicLab.Common/3.3/TypeExtensions.cs

    r7259 r8310  
    4949
    5050    public static IEnumerable<FieldInfo> GetAllFields(this Type type) {
    51       foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
     51      foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic))
    5252        yield return field;
    5353
    54       foreach (var field in type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
     54      foreach (var field in type.GetFields(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic))
    5555        yield return field;
    5656
Note: See TracChangeset for help on using the changeset viewer.