main
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Reflection;
5
6namespace tests
7{
8 static class Reflecting
9 {
10 static public void class_hierarchy(this Type target, Action<Type> action)
11 {
12 action(target);
13 var base_class = target.BaseType;
14 if (null != base_class) base_class.class_hierarchy(action);
15 }
16
17 static public IEnumerable<FieldInfo> collect_fields_of_type<T>(this IReflect target)
18 {
19 return target
20 .GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
21 .Where(x => x.FieldType.Equals(typeof (T)));
22 }
23
24 static public void invoke_delegate_on(this FieldInfo field, object target)
25 {
26 field.GetValue(target).downcast_to<Delegate>().DynamicInvoke();
27 }
28
29 static public ConstructorInfo find_the_greediest_constructor(this Type type)
30 {
31 var all = type.GetConstructors();
32 ConstructorInfo greediest = null;
33 foreach (var constructor in all)
34 {
35 if (null == greediest)
36 {
37 greediest = constructor;
38 continue;
39 }
40 if (constructor.GetParameters().Length > greediest.GetParameters().Length)
41 {
42 greediest = constructor;
43 }
44 }
45 return greediest;
46 }
47 }
48}