forked from Astn/JSON-RPC.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonRpcService.cs
More file actions
65 lines (55 loc) · 3.04 KB
/
JsonRpcService.cs
File metadata and controls
65 lines (55 loc) · 3.04 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
namespace AustinHarris.JsonRpc
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AustinHarris.JsonRpc;
public abstract class JsonRpcService
{
protected JsonRpcService()
{
buildService(Handler.DefaultSessionId());
}
protected JsonRpcService(string sessionID)
{
buildService(sessionID);
}
private void buildService(string sessionID)
{
// get the registerMethod.
// Method that matches Func<T,R>
var regMethod = typeof(Handler).GetMethod("Register");
// var assem = Assembly.GetExecutingAssembly();
// var TypesWithHandlers = assem.GetTypes().Where(f => f.GetCustomAttributes(typeof(JsonRpcAttribute), false).Length > 0);
var item = this.GetType();
var methods = item.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(m => m.GetCustomAttributes(typeof(JsonRpcMethodAttribute), false).Length > 0);
foreach (var meth in methods)
{
Dictionary<string, Type> paras = new Dictionary<string, Type>();
Dictionary<string, object> defaultValues = new Dictionary<string, object>(); // dictionary that holds default values for optional params.
var paramzs = meth.GetParameters();
List<Type> parameterTypeArray = new List<Type>();
for (int i = 0; i < paramzs.Length; i++)
{
// reflection attribute information for optional parameters
//http://stackoverflow.com/questions/2421994/invoking-methods-with-optional-parameters-through-reflection
paras.Add(paramzs[i].Name, paramzs[i].ParameterType);
if (paramzs[i].IsOptional) // if the parameter is an optional, add the default value to our default values dictionary.
defaultValues.Add(paramzs[i].Name, paramzs[i].DefaultValue);
}
var resType = meth.ReturnType;
paras.Add("returns", resType); // add the return type to the generic parameters list.
var atdata = meth.GetCustomAttributes(typeof(JsonRpcMethodAttribute), false);
foreach (JsonRpcMethodAttribute handlerAttribute in atdata)
{
var methodName = handlerAttribute.JsonMethodName == string.Empty ? meth.Name : handlerAttribute.JsonMethodName;
var newDel = Delegate.CreateDelegate(System.Linq.Expressions.Expression.GetDelegateType(paras.Values.ToArray()), this /*Need to add support for other methods outside of this instance*/, meth);
var handlerSession = Handler.GetSessionHandler(sessionID);
regMethod.Invoke(handlerSession, new object[] { methodName, newDel });
handlerSession.MetaData.AddService(methodName, paras, defaultValues);
}
}
}
}
}