-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionRouter.cs
193 lines (159 loc) · 7.68 KB
/
ActionRouter.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
187
188
189
190
191
192
193
using System;
using System.Reflection;
using System.Text.RegularExpressions;
//http://dotnetslackers.com/Community/blogs/haissam/archive/2007/07/25/Call-a-function-using-Reflection.aspx
namespace Maussoft.Mvc
{
public class ActionRouter<TSession> where TSession : new()
{
string controllerNamespace;
public ActionRouter(string controllerNamespace)
{
this.controllerNamespace = controllerNamespace;
}
private Boolean Invoke(WebContext<TSession> context, string className, Type routedClass, MethodInfo routedMethod, object[] parameters)
{
object routedObject = Activator.CreateInstance(routedClass);
if (routedObject == null)
{
Console.WriteLine("ActionRouter: object {0} could not be created.", routedClass.FullName);
return false;
}
Console.WriteLine("ActionRouter: object {0} was created.", routedClass.FullName);
context.View = className + '.' + routedMethod.Name;
context.Controller = className;
context.Action = routedMethod.Name;
routedMethod.Invoke(routedObject, parameters);
Console.WriteLine("ActionRouter: invoked {0}.{1}(...)", routedClass.FullName, routedMethod.Name);
return true;
}
private object[] GetParameters(WebContext<TSession> context, ParameterInfo[] parameterList, ArraySegment<string> arguments)
{
object[] parameters = new object[parameterList.Length];
parameters[0] = context;
for (int p = 1; p < parameterList.Length; p++)
{
if (arguments.Offset + p - 1 < arguments.Array.Length)
{
string[] ints = new string[] { "System.Byte", "System.SByte", "System.Int32", "System.UInt32", "System.Int16", "System.UInt16", "System.Int64", "System.UInt64" };
try
{
if (parameterList[p].ParameterType.ToString() == "System.String")
{
parameters[p] = arguments.Array[arguments.Offset + p - 1];
}
else if (Array.IndexOf(ints, parameterList[p].ParameterType.ToString()) >= 0)
{
string value = arguments.Array[arguments.Offset + p - 1];
value = Regex.Replace(value, @"[^0-9]+", "");
parameters[p] = Convert.ChangeType(value, parameterList[p].ParameterType);
}
else
{
Console.WriteLine("ActionRouter: non-compatible type for argument #{0}: {1} ({2})", p, parameterList[p].Name, parameterList[p].ParameterType);
return null;
}
}
catch (System.FormatException)
{
Console.WriteLine("ActionRouter: conversion failed for argument #{0}: {1} ({2})", p, parameterList[p].Name, parameterList[p].ParameterType);
return null;
}
Console.WriteLine("ActionRouter: converted string to '{2}' for argument #{0}: {1}", p, parameterList[p].Name, parameterList[p].ParameterType);
}
else
{
if (parameterList[p].IsOptional)
{
parameters[p] = parameterList[p].DefaultValue;
Console.WriteLine("ActionRouter: default value added for missing argument #{0}: {1}", p, parameterList[p].Name);
}
else
{
Console.WriteLine("ActionRouter: missing non-optional argument #{0}: {1}", p, parameterList[p].Name);
return null;
}
}
}
return parameters;
}
private Boolean Match(WebContext<TSession> context, string prefix, string className, string methodName, ArraySegment<string> arguments)
{
Type routedClass = null;
MethodInfo routedMethod = null;
object[] parameters = null;
ParameterInfo[] parameterList = null;
Assembly assembly = Assembly.GetEntryAssembly();
Console.WriteLine("ActionRouter: try {0}.{1}(...{2})", className, methodName, arguments.Count);
routedClass = assembly.GetType(prefix + '.' + className);
if (routedClass == null)
{
Console.WriteLine("ActionRouter: class {0} does not exist.", prefix + '.' + className);
return false;
}
Console.WriteLine("ActionRouter: class {0} found.", prefix + '.' + className);
routedMethod = routedClass.GetMethod(methodName);
if (routedMethod == null)
{
Console.WriteLine("Routing: method {0}.{1} does not exist.", className, methodName);
return false;
}
Console.WriteLine("ActionRouter: method {0}.{1} found.", className, methodName);
parameterList = routedMethod.GetParameters();
if (parameterList.Length < 1)
{
Console.WriteLine("ActionRouter: first argument not found");
return false;
}
Console.WriteLine("ActionRouter: first argument found");
if (parameterList[0].ParameterType != context.GetType())
{
Console.WriteLine("ActionRouter: first argument type mismatch {0} != {1}", parameterList[0].ParameterType, context.GetType());
return false;
}
Console.WriteLine("ActionRouter: first argument type matches");
if (arguments.Count > parameterList.Length - 1)
{
Console.WriteLine("ActionRouter: argument count too high: {0} > {1}", arguments.Count, parameterList.Length - 1);
return false;
}
Console.WriteLine("ActionRouter: argument count not too high");
parameters = GetParameters(context, parameterList, arguments);
if (parameters == null)
{
Console.WriteLine("ActionRouter: invalid argument encountered");
return false;
}
return this.Invoke(context, className, routedClass, routedMethod, parameters);
}
public Boolean Route(WebContext<TSession> context)
{
string[] parts = context.Url.TrimStart('/').Split('/');
string className = null;
string methodName = null;
ArraySegment<string> arguments = new ArraySegment<string>();
string prefix = this.controllerNamespace;
for (int i = parts.Length; i >= 1; i--)
{
if (i < parts.Length)
{
className = String.Join(".", parts, 0, i);
methodName = parts[i];
arguments = new ArraySegment<string>(parts, i + 1, parts.Length - (i + 1));
if (this.Match(context, prefix, className, methodName, arguments))
{
return true;
}
}
className = String.Join(".", parts, 0, i);
methodName = "Index";
arguments = new ArraySegment<string>(parts, i, parts.Length - i);
if (this.Match(context, prefix, className, methodName, arguments))
{
return true;
}
}
return false;
}
}
}