I am trying to write a function which can take in a URI and then map that to a function in C#. The intention is to have a single object in C# which has a number of functions or members which can be called. This would be similar to the Method Dispatcher in CherryPy.
I am most of the way there, but am stuck at grabbing the object in C# when reflecting through multiple layers. At current I have a function which will recurs through several layers of member object but this is done using types, and not the direct objects.
I find this hard to explain so hopefully the code below will clarify the issue (I have removed all error checking and the out clause).
The problem I have is that when calling MethodInfo.Invoke, I do not have the physical object reference to pass in as the first parameter.
I believe I should be able to get the object reference somehow from "fieldInfo" If somebody can tell me how to do that I believe I can complete this function.
{
List<String> path = request.Url.AbsolutePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries).ToList<String>();
object[] parameters = new object[2] { request, response };
System.Type controllerType = Controller.GetType();
MethodInfo methodInfo = GetMethod(path, controllerType);
methodInfo.Invoke(<I DONT HAVE THIS OBJECT>, parameters);
}
private MethodInfo GetMethod(List<String> path, System.Type type)
{
MethodInfo methodInfo = type.GetMethod(path[0], BindingFlags.Public | BindingFlags.Instance);
if(methodInfo != null)
{
return methodInfo;
}
FieldInfo fieldInfo = type.GetField(path[0].ToString());
path.RemoveAt(0);
return GetMethod(path, fieldInfo.FieldType);
}