-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFakerRoutes.cs
51 lines (44 loc) · 1.75 KB
/
FakerRoutes.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
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
namespace Bogus.Web
{
public static class FakerRoutes
{
public static void Configure(IRouteBuilder routes)
{
var fakerType = typeof(Faker);
var properties = fakerType.GetProperties(
BindingFlags.GetProperty
| BindingFlags.Public
| BindingFlags.Instance)
.Where(p => typeof(DataSet).IsAssignableFrom(p.PropertyType));
foreach (var property in properties)
{
var methods = property.PropertyType
.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(m => m.DeclaringType == property.PropertyType)
.Where(m => !m.IsSpecialName);
foreach (var method in methods)
{
var parameters = method.GetParameters();
var template = $"{property.Name}/{method.Name}";
if (parameters.Any())
{
if (parameters.Any(p => !p.ParameterType.IsValueType && p.ParameterType != typeof(string)))
continue;
template +=
$"/{string.Join("/", parameters.Select(p => $"{{{p.Name}{(p.IsOptional ? "?" : "")}}}"))}";
}
routes.MapRoute(
template,
template,
new { controller = "Faker", action = "Generate" },
null,
new { property, method });
}
}
}
}
}