forked from bonesoul/uhttpsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionHandlerFactory.cs
More file actions
45 lines (42 loc) · 1.6 KB
/
ReflectionHandlerFactory.cs
File metadata and controls
45 lines (42 loc) · 1.6 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace uhttpsharp
{
public class ReflectionHandlerFactory : IHandlerFactory
{
private readonly Dictionary<string, HttpRequestHandler> _handlers = new Dictionary<string, HttpRequestHandler>();
HttpRequestHandler IHandlerFactory.GetHandler(string name)
{
HttpRequestHandler handler;
if (_handlers.TryGetValue(name, out handler))
return handler;
return null;
}
public ReflectionHandlerFactory()
{
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes());
foreach (var t in types)
{
if (t.IsSubclassOf(typeof(HttpRequestHandler)))
{
Console.WriteLine("found RequestHandler {0}", t.Name);
try
{
var attributes = t.GetCustomAttributes(typeof(HttpRequestHandlerAttributes), true);
if (attributes.Length > 0)
{
var handler = (HttpRequestHandler)Activator.CreateInstance(t);
_handlers.Add(((HttpRequestHandlerAttributes)attributes[0]).Function, handler);
}
}
catch (Exception e)
{
Console.WriteLine(string.Format("Exception during activating the IHttpRequestHandler: {0} - {1}", t, e));
}
}
}
}
}
}