- Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathServiceBinder.cs
More file actions
Latest commit
71 lines (63 loc) · 3.31 KB
/
Copy pathServiceBinder.cs
File metadata and controls
71 lines (63 loc) · 3.31 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
66
67
68
69
70
71
namespaceAustinHarris.JsonRpc
{
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Reflection;
usingAustinHarris.JsonRpc;
publicstaticclassServiceBinder
{
publicstaticvoidBindService<T>()whereT:new()
{
BindService<T>(Handler.DefaultSessionId());
}
publicstaticvoidBindService<T>(stringsessionID)whereT:new()
{
BindService(sessionID,newT());
}
publicstaticvoidBindService(stringsessionID,Objectinstance)
{
varitem=instance.GetType();// var item = typeof(T);
varmethods=item.GetMethods(BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.Instance).Where(m =>m.GetCustomAttributes(typeof(JsonRpcMethodAttribute),false).Length>0);
foreach(varmethinmethods)
{
Dictionary<string,Type>paras=newDictionary<string,Type>();
Dictionary<string,object>defaultValues=newDictionary<string,object>();// dictionary that holds default values for optional params.
varparamzs=meth.GetParameters();
List<Type>parameterTypeArray=newList<Type>();
for(inti=0;i<paramzs.Length;i++)
{
stringparamName;
varparamAttrs=paramzs[i].GetCustomAttributes(typeof(JsonRpcParamAttribute),false);
if(paramAttrs.Length>0)
{
paramName=((JsonRpcParamAttribute)paramAttrs[0]).JsonParamName;
if(string.IsNullOrEmpty(paramName))
{
paramName=paramzs[i].Name;
}
}
else
{
paramName=paramzs[i].Name;
}
// reflection attribute information for optional parameters
//http://stackoverflow.com/questions/2421994/invoking-methods-with-optional-parameters-through-reflection
paras.Add(paramName,paramzs[i].ParameterType);
if(paramzs[i].IsOptional)// if the parameter is an optional, add the default value to our default values dictionary.
defaultValues.Add(paramName,paramzs[i].DefaultValue);
}
varresType=meth.ReturnType;
paras.Add("returns",resType);// add the return type to the generic parameters list.
varatdata=meth.GetCustomAttributes(typeof(JsonRpcMethodAttribute),false);
foreach(JsonRpcMethodAttributehandlerAttributeinatdata)
{
varmethodName=handlerAttribute.JsonMethodName==string.Empty?meth.Name:handlerAttribute.JsonMethodName;
varnewDel=Delegate.CreateDelegate(System.Linq.Expressions.Expression.GetDelegateType(paras.Values.ToArray()),instance/*Need to add support for other methods outside of this instance*/,meth);
varhandlerSession=Handler.GetSessionHandler(sessionID);
handlerSession.MetaData.AddService(methodName,paras,defaultValues,newDel);
}
}
}
}
}