Skip to content

Commit e58f5d6

Browse files
committed
Possibility to specify a method parameter name as it will be referred to by JsonRpc
1 parent c0a6d05 commit e58f5d6

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

Json-Rpc/Attributes.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,27 @@ public string JsonMethodName
2424
get { return jsonMethodName; }
2525
}
2626
}
27+
28+
/// <summary>
29+
/// Used to assign JsonRpc parameter name to method argument.
30+
/// </summary>
31+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false, AllowMultiple = false)]
32+
public sealed class JsonRpcParamAttribute : Attribute
33+
{
34+
readonly string jsonParamName;
35+
36+
/// <summary>
37+
/// Used to assign JsonRpc parameter name to method argument.
38+
/// </summary>
39+
/// <param name="jsonParamName">Lets you specify the parameter name as it will be referred to by JsonRpc.</param>
40+
public JsonRpcParamAttribute(string jsonParamName = "")
41+
{
42+
this.jsonParamName = jsonParamName;
43+
}
44+
45+
public string JsonParamName
46+
{
47+
get { return jsonParamName; }
48+
}
49+
}
2750
}

Json-Rpc/JsonRpcService.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,25 @@ private void buildService(string sessionID)
3737
var paramzs = meth.GetParameters();
3838

3939
List<Type> parameterTypeArray = new List<Type>();
40-
for (int i = 0; i < paramzs.Length; i++)
40+
for (int i = 0; i < paramzs.Length; i++)
4141
{
42+
string paramName;
43+
var paramAttrs = paramzs[i].GetCustomAttributes(typeof(JsonRpcParamAttribute), false);
44+
if (paramAttrs.Length > 0)
45+
{
46+
paramName = ((JsonRpcParamAttribute)paramAttrs[0]).JsonParamName;
47+
}
48+
else
49+
{
50+
paramName = paramzs[i].Name;
51+
}
52+
4253
// reflection attribute information for optional parameters
4354
//http://stackoverflow.com/questions/2421994/invoking-methods-with-optional-parameters-through-reflection
44-
paras.Add(paramzs[i].Name, paramzs[i].ParameterType);
55+
paras.Add(paramName, paramzs[i].ParameterType);
4556

4657
if (paramzs[i].IsOptional) // if the parameter is an optional, add the default value to our default values dictionary.
47-
defaultValues.Add(paramzs[i].Name, paramzs[i].DefaultValue);
58+
defaultValues.Add(paramName, paramzs[i].DefaultValue);
4859
}
4960

5061
var resType = meth.ReturnType;

0 commit comments

Comments
 (0)