Free cookie consent management tool by TermsFeed Policy Generator

Changeset 9855 for trunk


Ignore:
Timestamp:
08/06/13 16:36:11 (11 years ago)
Author:
jkarder
Message:

#2095

  • added FieldInfo cache
  • refactored GetAllFields to work iteratively
Location:
trunk/sources/HeuristicLab.Common/3.3
Files:
2 edited

Legend:

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

    r9456 r9855  
    3838      if (obj == null) return Enumerable.Empty<object>();
    3939      if (excludedMembers == null) excludedMembers = new HashSet<object>();
     40      var fieldInfos = new Dictionary<Type, FieldInfo[]>();
    4041
    4142      var objects = new HashSet<object>();
     
    4748        objects.Add(current);
    4849
    49         foreach (object o in GetChildObjects(current, excludedMembers, excludeStaticMembers)) {
     50        foreach (object o in GetChildObjects(current, excludedMembers, excludeStaticMembers, fieldInfos)) {
    5051          if (o == null) continue;
    5152          if (ExcludeType(o.GetType())) continue;
     
    8081    }
    8182
    82     private static IEnumerable<object> GetChildObjects(object obj, HashSet<object> excludedMembers, bool excludeStaticMembers) {
     83    private static IEnumerable<object> GetChildObjects(object obj, HashSet<object> excludedMembers, bool excludeStaticMembers, Dictionary<Type, FieldInfo[]> fieldInfos) {
    8384      Type type = obj.GetType();
    8485
     
    111112        }
    112113      } else {
    113         foreach (FieldInfo f in type.GetAllFields()) {
     114        if (!fieldInfos.ContainsKey(type))
     115          fieldInfos[type] = type.GetAllFields().ToArray();
     116        foreach (FieldInfo f in fieldInfos[type]) {
    114117          if (excludeStaticMembers && f.IsStatic) continue;
    115118          object fieldValue;
  • trunk/sources/HeuristicLab.Common/3.3/TypeExtensions.cs

    r9456 r9855  
    4949
    5050    public static IEnumerable<FieldInfo> GetAllFields(this Type type) {
    51       foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic))
    52         yield return field;
    53 
    54       foreach (var field in type.GetFields(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic))
    55         yield return field;
    56 
    57       if (type.BaseType != null) {
    58         foreach (var field in type.BaseType.GetAllFields())
     51      while (type != null) {
     52        foreach (var field in type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic))
    5953          yield return field;
     54        type = type.BaseType;
    6055      }
    6156    }
Note: See TracChangeset for help on using the changeset viewer.