-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleDescriber.cs
83 lines (78 loc) · 3.47 KB
/
RuleDescriber.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System.Collections.Generic;
using System.Reflection;
using ValidationFramework.Extensions;
using ValidationFramework.Reflection;
namespace ValidationFramework
{
/// <summary>
/// Provides access to all <see cref="TypeDescriptor"/>s for an <see cref="Assembly"/>.
/// </summary>
public static class RuleDescriber
{
/// <summary>
/// Read all the <see cref="TypeDescriptor"/>s for an <see cref="Assembly"/>.
/// </summary>
/// <param name="assembly">The <see cref="Assembly"/> to read attributes from.</param>
/// <returns>All the <see cref="TypeDescriptor"/>s for an <see cref="Assembly"/>.</returns>
public static IList<TypeDescriptor> GetPropertyRules(Assembly assembly)
{
var list = new List<TypeDescriptor>();
var types = assembly.GetTypes();
for (var typeIndex = 0; typeIndex < types.Length; typeIndex++)
{
var type = types[typeIndex];
//we only care about classes.
if (type.IsClass)
{
var typeDescriptor = TypeCache.GetType(type);
if (typeDescriptor != null)
{
foreach (var propertyDescriptor in typeDescriptor.Properties)
{
if (propertyDescriptor.Rules.Count > 0)
{
list.Add(typeDescriptor);
break;
}
}
}
}
}
return list;
}
/// <summary>
/// Read all the <see cref="MethodDescriptor"/>s for an <see cref="Assembly"/>.
/// </summary>
/// <param name="assembly">The <see cref="Assembly"/> to read attributes from.</param>
/// <returns>All the <see cref="MethodDescriptor"/>s for an <see cref="Assembly"/>.</returns>
public static IList<MethodDescriptor> GetMethodRules(Assembly assembly)
{
var list = new List<MethodDescriptor>();
for (var typeIndex = 0; typeIndex < assembly.GetTypes().Length; typeIndex++)
{
var type = assembly.GetTypes()[typeIndex];
//we only care about classes and interfaces.
if (type.IsClass || type.IsInterface)
{
foreach (var methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance))
{
//TODO: support generic methods
if (!TypeExtensions.IsPropertyMethod(methodInfo) && !methodInfo.ContainsGenericParameters && methodInfo.GetParameters().Length > 0)
{
var descriptor = MethodCache.GetMethod(methodInfo.MethodHandle);
foreach (var parameter in descriptor.Parameters)
{
if (parameter.Rules.Count > 0)
{
list.Add(descriptor);
break;
}
}
}
}
}
}
return list;
}
}
}