Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/14/11 16:45:46 (13 years ago)
Author:
abeham
Message:

#1465

  • updated branch with latest version of trunk
Location:
branches/histogram
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/histogram

  • branches/histogram/HeuristicLab.Common/3.3/TypeExtensions.cs

    r5445 r6195  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Reflection;
    2325using System.Text;
    2426
     
    4547      return sb.ToString();
    4648    }
     49
     50    public static IEnumerable<FieldInfo> GetAllFields(this Type type) {
     51      foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
     52        yield return field;
     53
     54      foreach (var field in type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
     55        yield return field;
     56
     57      if (type.BaseType != null) {
     58        foreach (var field in type.BaseType.GetAllFields())
     59          yield return field;
     60      }
     61    }
     62
     63    // http://stackoverflow.com/questions/457676/c-reflection-check-if-a-class-is-derived-from-a-generic-class
     64    public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic) {
     65      while (toCheck != typeof(object)) {
     66        var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
     67        if (generic == cur) {
     68          return true;
     69        }
     70        toCheck = toCheck.BaseType; // baseType is null when toCheck is an Interface
     71        if (toCheck == null)
     72          return false;
     73      }
     74      return false;
     75    }
    4776  }
    4877}
Note: See TracChangeset for help on using the changeset viewer.