forked from NancyFx/Nancy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
InteractiveDiagnostics.cs
186 lines (145 loc) · 7.43 KB
/
InteractiveDiagnostics.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
namespace Nancy.Diagnostics
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
public class InteractiveDiagnostics : IInteractiveDiagnostics
{
private readonly IDiagnosticsProvider[] providers;
private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
public IEnumerable<InteractiveDiagnostic> AvailableDiagnostics { get; private set; }
public InteractiveDiagnostics(IEnumerable<IDiagnosticsProvider> providers)
{
var customProvidersAvailable = providers.Any(provider =>
{
Type providerType = provider.GetType();
return providerType != typeof(Nancy.Diagnostics.TestingDiagnosticProvider) &
providerType != typeof(Nancy.Routing.DefaultRouteCacheProvider);
});
if (customProvidersAvailable)
{
// Exclude only the TestingDiagnosticProvider
this.providers = providers.Where(provider => provider.GetType() != typeof(Nancy.Diagnostics.TestingDiagnosticProvider)).ToArray();
}
else
{
this.providers = providers.ToArray();
}
this.BuildAvailableDiagnostics();
}
public object ExecuteDiagnostic(InteractiveDiagnosticMethod interactiveDiagnosticMethod, object[] arguments)
{
var method = GetMethodInfo(interactiveDiagnosticMethod);
if (method == null)
{
throw new ArgumentException(string.Format("Unable to locate method: {0}", interactiveDiagnosticMethod.MethodName));
}
return method.Invoke(interactiveDiagnosticMethod.ParentDiagnosticObject, arguments);
}
public string GetTemplate(InteractiveDiagnosticMethod interactiveDiagnosticMethod)
{
var diagObjectType = interactiveDiagnosticMethod.ParentDiagnosticObject.GetType();
return GetTemplateFromProperty(interactiveDiagnosticMethod, diagObjectType) ??
GetTemplateFromAttribute(interactiveDiagnosticMethod);
}
public InteractiveDiagnostic GetDiagnostic(string providerName)
{
return this.AvailableDiagnostics.FirstOrDefault(d => string.Equals(d.Name, providerName, StringComparison.OrdinalIgnoreCase));
}
public InteractiveDiagnosticMethod GetMethod(string providerName, string methodName)
{
var diagnostic = this.GetDiagnostic(providerName);
if (diagnostic == null)
{
return null;
}
return diagnostic.Methods.FirstOrDefault(m => string.Equals(m.MethodName, methodName, StringComparison.OrdinalIgnoreCase));
}
private void BuildAvailableDiagnostics()
{
var diags = new List<InteractiveDiagnostic>(this.providers.Length);
foreach (var diagnosticsProvider in this.providers)
{
diags.Add(new InteractiveDiagnostic
{
Name = diagnosticsProvider.Name,
Description = diagnosticsProvider.Description,
Methods = this.GetDiagnosticMethods(diagnosticsProvider)
});
}
this.AvailableDiagnostics = diags;
}
private IEnumerable<InteractiveDiagnosticMethod> GetDiagnosticMethods(IDiagnosticsProvider diagnosticsProvider)
{
var methods = diagnosticsProvider.DiagnosticObject
.GetType()
.GetMethods(Flags)
.Where(mi => ! mi.IsSpecialName).ToArray();
var diagnosticMethods = new List<InteractiveDiagnosticMethod>(methods.Length);
foreach (var methodInfo in methods)
{
diagnosticMethods.Add(new InteractiveDiagnosticMethod(
diagnosticsProvider.DiagnosticObject,
methodInfo.ReturnType,
methodInfo.Name,
this.GetArguments(methodInfo),
this.GetDescription(diagnosticsProvider, methodInfo)));
}
return diagnosticMethods;
}
private string GetDescription(IDiagnosticsProvider diagnosticsProvider, MethodInfo methodInfo)
{
return GetDescriptionFromProperty(diagnosticsProvider, methodInfo) ??
GetDescriptionFromAttribute(diagnosticsProvider, methodInfo);
}
private IEnumerable<Tuple<string, Type>> GetArguments(MethodInfo methodInfo)
{
var parameters = methodInfo.GetParameters();
var arguments = new List<Tuple<string, Type>>(parameters.Length);
foreach (var parameterInfo in parameters)
{
arguments.Add(Tuple.Create(parameterInfo.Name, parameterInfo.ParameterType));
}
return arguments;
}
private static string GetTemplateFromProperty(
InteractiveDiagnosticMethod interactiveDiagnosticMethod, Type diagObjectType)
{
var propertyName = String.Format("{0}{1}", interactiveDiagnosticMethod.MethodName, "Template");
var property = diagObjectType.GetProperty(propertyName);
if (property == null)
{
return null;
}
return (string)property.GetValue(interactiveDiagnosticMethod.ParentDiagnosticObject, null);
}
private static string GetTemplateFromAttribute(InteractiveDiagnosticMethod interactiveDiagnosticMethod)
{
var method = GetMethodInfo(interactiveDiagnosticMethod);
var attribute = (TemplateAttribute)Attribute.GetCustomAttribute(method, typeof(TemplateAttribute));
return attribute != null ? attribute.Template : null;
}
private static string GetDescriptionFromProperty(IDiagnosticsProvider diagnosticsProvider, MethodInfo methodInfo)
{
var propertyName = String.Format("{0}{1}", methodInfo.Name, "Description");
var property = diagnosticsProvider.DiagnosticObject.GetType().GetProperty(propertyName);
if (property == null)
{
return null;
}
return (string)property.GetValue(diagnosticsProvider.DiagnosticObject, null);
}
private static string GetDescriptionFromAttribute(IDiagnosticsProvider diagnosticsProvider, MethodInfo methodInfo)
{
var attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(methodInfo, typeof(DescriptionAttribute));
return attribute != null ? attribute.Description : null;
}
private static MethodInfo GetMethodInfo(InteractiveDiagnosticMethod interactiveDiagnosticMethod)
{
var diagObjectType = interactiveDiagnosticMethod.ParentDiagnosticObject.GetType();
var method = diagObjectType.GetMethod(interactiveDiagnosticMethod.MethodName, Flags);
return method;
}
}
}