WIP
parent
96388a8e16
commit
97ff209cb7
|
@ -14,6 +14,9 @@ using Newtonsoft.Json;
|
|||
using ln.http.exceptions;
|
||||
using ln.logging;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
namespace ln.http.resources
|
||||
{
|
||||
public class CallableAttribute : Attribute
|
||||
|
@ -25,6 +28,8 @@ namespace ln.http.resources
|
|||
public abstract class JsonCallResource : BaseResource
|
||||
{
|
||||
Dictionary<string, MethodInfo[]> callableMethods = new Dictionary<string, MethodInfo[]>();
|
||||
Dictionary<string, MethodResource> methodResources = new Dictionary<string, MethodResource>();
|
||||
|
||||
|
||||
public JsonCallResource(Resource container, string name)
|
||||
: base(container, name)
|
||||
|
@ -53,10 +58,34 @@ namespace ln.http.resources
|
|||
foreach (String alias in callables.Keys)
|
||||
{
|
||||
callableMethods.Add(alias, callables[alias].ToArray());
|
||||
new MethodResource(this, alias);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private object InvokeMethodCall(string methodName, object[] arguments)
|
||||
{
|
||||
MethodInfo methodInfo = FindMethodSignature(methodName, arguments.Length);
|
||||
return methodInfo.Invoke(this, arguments);
|
||||
}
|
||||
|
||||
private object InvokeMethodCall(string methodName, KeyValuePair<string,object>[] arguments)
|
||||
{
|
||||
MethodInfo methodInfo = FindMethodSignature(methodName, arguments.Select((kvp) => kvp.Key).ToArray());
|
||||
Dictionary<string, object> args = new Dictionary<string, object>();
|
||||
foreach (KeyValuePair<string,object> kvp in arguments)
|
||||
args.Add(kvp.Key, kvp.Value);
|
||||
|
||||
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
|
||||
object[] pl = new object[parameterInfos.Length];
|
||||
|
||||
for (int n=0;n<parameterInfos.Length;n++)
|
||||
pl[n] = args[parameterInfos[n].Name];
|
||||
|
||||
return methodInfo.Invoke(this, pl);
|
||||
}
|
||||
|
||||
|
||||
private MethodInfo FindMethodSignature(String methodName,int nParameters)
|
||||
{
|
||||
foreach (MethodInfo methodInfo in callableMethods[methodName])
|
||||
|
@ -69,6 +98,28 @@ namespace ln.http.resources
|
|||
throw new ArgumentException("No method signature matching the parameters was found");
|
||||
}
|
||||
|
||||
private MethodInfo FindMethodSignature(String methodName, String[] argumentNames)
|
||||
{
|
||||
foreach (MethodInfo methodInfo in callableMethods[methodName])
|
||||
{
|
||||
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
|
||||
if (parameterInfos.Length == argumentNames.Length)
|
||||
{
|
||||
int n;
|
||||
for (n = 0; n < parameterInfos.Length; n++)
|
||||
{
|
||||
if (!argumentNames.Contains(parameterInfos[n].Name))
|
||||
break;
|
||||
}
|
||||
|
||||
if (n == argumentNames.Length)
|
||||
return methodInfo;
|
||||
}
|
||||
}
|
||||
throw new ArgumentException("No method signature matching the parameters was found");
|
||||
}
|
||||
|
||||
|
||||
public override HttpResponse GetResponse(HttpRequest httpRequest)
|
||||
{
|
||||
if (!httpRequest.GetRequestHeader("Content-Type").Equals("application/json"))
|
||||
|
@ -85,7 +136,15 @@ namespace ln.http.resources
|
|||
methodResult.MethodName = methodCall.MethodName;
|
||||
|
||||
MethodInfo methodInfo = FindMethodSignature(methodCall.MethodName, methodCall.Parameters.Length);
|
||||
methodResult.Result = methodInfo.Invoke(this, methodCall.Parameters);
|
||||
ParameterInfo[] mp = methodInfo.GetParameters();
|
||||
|
||||
object[] arguments = new object[methodCall.Parameters.Length];
|
||||
|
||||
for (int n=0;n<methodCall.Parameters.Length; n++)
|
||||
{
|
||||
arguments[n] = Convert.ChangeType(methodCall.Parameters[n], mp[n].ParameterType);
|
||||
}
|
||||
methodResult.Result = methodInfo.Invoke(this, arguments);
|
||||
|
||||
|
||||
|
||||
|
@ -161,5 +220,25 @@ namespace ln.http.resources
|
|||
public Exception Exception;
|
||||
}
|
||||
|
||||
|
||||
class MethodResource : Resource
|
||||
{
|
||||
public MethodResource(JsonCallResource container,String methodName)
|
||||
:base(container,methodName)
|
||||
{
|
||||
}
|
||||
|
||||
public override HttpResponse GetResponse(HttpRequest httpRequest)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
public override void AddResource(Resource resource) => throw new NotImplementedException();
|
||||
public override bool Contains(string name) => throw new NotImplementedException();
|
||||
public override IEnumerable<Resource> GetResources() => throw new NotImplementedException();
|
||||
public override void RemoveResource(Resource resource) => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue