Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/21/16 09:53:55 (8 years ago)
Author:
gkronber
Message:

#2626: merged r13998, r14032 and r14144 from trunk to stable

Location:
stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Common/3.3/ObjectExtensions.cs

    r13879 r14145  
    3030
    3131namespace HeuristicLab.Common {
     32
     33  [AttributeUsage(AttributeTargets.Field)]
     34  // this attribute can be used to mark fields that should be excluded from object graph traversal
     35  public class ExcludeFromObjectGraphTraversalAttribute : Attribute {
     36  }
     37
    3238  public static class ObjectExtensions {
    3339    public static IEnumerable<T> ToEnumerable<T>(this T obj) {
     
    113119        }
    114120      } else {
    115         if (!fieldInfos.ContainsKey(type))
    116           fieldInfos[type] = type.GetAllFields().ToArray();
    117         foreach (FieldInfo f in fieldInfos[type]) {
     121        FieldInfo[] fieldInfo;
     122        if (!fieldInfos.TryGetValue(type, out fieldInfo)) {
     123          fieldInfo = type.GetAllFields()
     124            .Where(fi => !Attribute.IsDefined(fi, typeof(ExcludeFromObjectGraphTraversalAttribute)))
     125            .ToArray();
     126          fieldInfos.Add(type, fieldInfo);
     127        }
     128        foreach (FieldInfo f in fieldInfo) {
    118129          if (excludeStaticMembers && f.IsStatic) continue;
    119130          object fieldValue;
    120131          try {
    121132            fieldValue = f.GetValue(obj);
    122           }
    123           catch (SecurityException) {
     133          } catch (SecurityException) {
    124134            continue;
    125135          }
Note: See TracChangeset for help on using the changeset viewer.