Beta 4.4.

master
Harald Wolff 2019-04-04 00:50:36 +02:00
parent 0b186370be
commit 0509cc37c0
1 changed files with 48 additions and 15 deletions

View File

@ -62,6 +62,14 @@ namespace ln.http.resources
new MethodResource(this, alias);
}
foreach (PropertyInfo propertyInfo in this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (propertyInfo.GetCustomAttribute<CallableAttribute>() != null)
{
new CallableProperty(this, propertyInfo);
}
}
}
private object InvokeMethodCall(string methodName, object[] arguments)
@ -152,22 +160,7 @@ namespace ln.http.resources
methodResult = new MethodResult();
methodResult.MethodName = methodCall.MethodName;
//MethodInfo methodInfo = FindMethodSignature(methodCall.MethodName, methodCall.Parameters.Length);
//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);
methodResult.Result = InvokeMethodCall(methodCall.MethodName, methodCall.Parameters);
} catch (Exception e)
{
methodResult = new MethodResult();
@ -260,5 +253,45 @@ namespace ln.http.resources
public override void RemoveResource(Resource resource) => throw new NotImplementedException();
}
class CallableProperty : Resource
{
public CallableProperty(JsonCallResource container, PropertyInfo propertyInfo)
: base(container, propertyInfo.Name)
{
this.propertyInfo = propertyInfo;
}
PropertyInfo propertyInfo;
public override void AddResource(Resource resource) => throw new NotImplementedException();
public override void RemoveResource(Resource resource) => throw new NotImplementedException();
public override bool Contains(string name) => false;
public override IEnumerable<Resource> GetResources() => new Resource[0];
public override HttpResponse GetResponse(HttpRequest httpRequest)
{
try
{
object v = propertyInfo.GetValue(Container);
String result = JsonConvert.SerializeObject(v);
HttpResponse httpResponse = new HttpResponse(httpRequest);
httpResponse.SetHeader("content-type", "application/json");
httpResponse.ContentWriter.Write(result);
return httpResponse;
} catch (Exception e)
{
Logging.Log(e);
HttpResponse httpResponse = new HttpResponse(httpRequest);
httpResponse.StatusCode = 500;
return httpResponse;
}
}
}
}
}