dimanche 7 août 2016

c# reflection - method invocation dont work

im writing a simple webserver with function mapping support in c#.

when i call a method with arguments it comes exception: The object does not match the target type.

but the type is successful converted to parameter type. my code:

private object[] MapParameter(HttpProcessor httpProcessor, MethodInfo m)
{
    var uri = new Uri(httpProcessor.http_url, UriKind.Relative);
    var ret = new List<object>();

    var query = HttpUtility.ParseQueryString(uri.OriginalString.Replace("/", ""));
    if (uri.OriginalString.Contains("?"))
    {
        foreach (var p in m.GetParameters())
        {
            var conv = TypeDescriptor.GetConverter(p.ParameterType);

            if (conv.CanConvertFrom(typeof (string)))
            {
                ret.Add(conv.ConvertFrom(query[p.Name]));
            }
        }
    }

    return ret.ToArray();
}

m is my annoated method as MethodInfo:

m.Invoke(ctrl, MapParameter(p, m));

and my method:

[Route]
public void Home(int my)
{
}





Aucun commentaire:

Enregistrer un commentaire