Пример #1
0
        internal static object ExecuteRequestedMethod(
            XmlRpcRequest request,
            XmlRpcMethodDescriptor methodDescriptor,
            ControllerBase controller)
        {
            var parameters         = new List <object>();
            var method             = methodDescriptor.MethodInfo;
            var requiredParameters = method.GetParameters();

            for (var i = 0; i < requiredParameters.Length; i++)
            {
                var parameter = requiredParameters[i];
                var type      = parameter.ParameterType;

                if (type.IsPrimitive())
                {
                    try
                    {
                        var obj = Convert.ChangeType(request.Parameters[i], type);
                        parameters.Add(obj);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        // Add the default value for the requested type.
                        parameters.Add(Activator.CreateInstance(type));
                    }
                    catch (Exception)
                    {
                        parameters.Add(request.Parameters[i]);
                    }
                }
                else if (type.IsArray)
                {
                    var elementType = type.GetElementType();

                    var arrayParameters =
                        (object[])request.Parameters[i];

                    var listType        = typeof(List <>);
                    var genericListType = listType.MakeGenericType(elementType);

                    var list = Activator.CreateInstance(genericListType);

                    foreach (Dictionary <string, object> values
                             in arrayParameters)
                    {
                        var element = Activator.CreateInstance(elementType);

                        foreach (var property
                                 in element.GetType().GetProperties())
                        {
                            var nameKey = property.GetSerializationName();

                            object value;
                            if (values.TryGetValue(nameKey, out value))
                            {
                                property.SetValue(element, value);
                            }
                        }

                        list.GetType()
                        .GetMethod("Add")
                        .Invoke(
                            list,
                            new[] { element });
                    }

                    parameters.Add(
                        list.GetType()
                        .GetMethod("ToArray")
                        .Invoke(list, null));
                }
                else
                {
                    var complexInstanceParameters =
                        (Dictionary <string, object>)request.Parameters[i];

                    var complexInstance =
                        Activator.CreateInstance(type);

                    foreach (var property in
                             complexInstance.GetType().GetProperties())
                    {
                        var nameKey = property.GetSerializationName();

                        object value;
                        if (complexInstanceParameters.TryGetValue(
                                nameKey,
                                out value))
                        {
                            property.SetValue(complexInstance, value);
                        }
                    }

                    parameters.Add(complexInstance);
                }
            }

            var instanceType = method.DeclaringType;

            if (method.DeclaringType != null &&
                method.DeclaringType.IsInterface)
            {
                instanceType = _s_getImplementation(method.DeclaringType);
            }

            var instance =
                controller.GetType() == instanceType
                    ? controller
                    : Activator.CreateInstance(instanceType);

            try
            {
                Trace.TraceInformation(
                    "XmlRpcMvc: Invoking method {0} with '{1}'",
                    method.Name,
                    string.Join(", ", parameters));

                return(method.Invoke(instance, parameters.ToArray()));
            }
            catch (XmlRpcFaultException exception)
            {
                return(exception);
            }
        }
        internal static object ExecuteRequestedMethod(
            XmlRpcRequest request,
            XmlRpcMethodDescriptor methodDescriptor,
            ControllerBase controller)
        {
            var parameters = new List<object>();
            var method = methodDescriptor.MethodInfo;
            var requiredParameters = method.GetParameters();

            for (var i = 0; i < requiredParameters.Length; i++)
            {
                var parameter = requiredParameters[i];
                var type = parameter.ParameterType;

                if (type.IsPrimitive())
                {
                    try
                    {
                        var obj = Convert.ChangeType(request.Parameters[i], type);
                        parameters.Add(obj);
                    }
                    catch (ArgumentOutOfRangeException)
                    {
                        // Add the default value for the requested type.
                        parameters.Add(Activator.CreateInstance(type));
                    }
                    catch (Exception)
                    {
                        parameters.Add(request.Parameters[i]);
                    }
                }
                else if (type.IsArray)
                {
                    var elementType = type.GetElementType();

                    var arrayParameters =
                        (object[])request.Parameters[i];

                    var listType = typeof(List<>);
                    var genericListType = listType.MakeGenericType(elementType);

                    var list = Activator.CreateInstance(genericListType);

                    foreach (Dictionary<string, object> values
                                in arrayParameters)
                    {
                        var element = Activator.CreateInstance(elementType);

                        foreach (var property
                                    in element.GetType().GetProperties())
                        {
                            var nameKey = property.GetSerializationName();

                            object value;
                            if (values.TryGetValue(nameKey, out value))
                            {
                                property.SetValue(element, value);
                            }
                        }

                        list.GetType()
                            .GetMethod("Add")
                            .Invoke(
                                list,
                                new[] { element });
                    }

                    parameters.Add(
                        list.GetType()
                            .GetMethod("ToArray")
                            .Invoke(list, null));
                }
                else
                {
                    var complexInstanceParameters =
                        (Dictionary<string, object>)request.Parameters[i];

                    var complexInstance =
                        Activator.CreateInstance(type);

                    foreach (var property in
                        complexInstance.GetType().GetProperties())
                    {
                        var nameKey = property.GetSerializationName();

                        object value;
                        if (complexInstanceParameters.TryGetValue(
                            nameKey,
                            out value))
                        {
                            property.SetValue(complexInstance, value);
                        }
                    }

                    parameters.Add(complexInstance);
                }
            }

            var instanceType = method.DeclaringType;

            if (method.DeclaringType != null &&
                method.DeclaringType.IsInterface)
            {
                instanceType = _s_getImplementation(method.DeclaringType);
            }

            var instance =
                controller.GetType() == instanceType
                    ? controller
                    : Activator.CreateInstance(instanceType);

            try
            {
                Trace.TraceInformation(
                    "XmlRpcMvc: Invoking method {0} with '{1}'",
                    method.Name,
                    string.Join(", ", parameters));

                return method.Invoke(instance, parameters.ToArray());
            }
            catch (XmlRpcFaultException exception)
            {
                return exception;
            }
        }