Exemplo n.º 1
0
        public object InvokeMethodWithJson(object obj, string serviceName, string[] Parameters)
        {
            var methodInfo       = obj.GetType().GetMethod(serviceName);
            var methodParameters = methodInfo.GetParameters();
            var convertedParams  = new object[methodParameters.Length];

            for (int i = 0; i < methodParameters.Length; i++)
            {
                try
                {
                    convertedParams[i] = Newtonsoft.Json.JsonConvert.DeserializeObject(Parameters[i], methodParameters[i].ParameterType);
                }
                catch
                {
                    if (methodParameters[i].ParameterType.IsPrimitiveType())
                    {
                        convertedParams[i] = Convert.ChangeType(Parameters[i], methodParameters[i].ParameterType);
                    }
                    else
                    {
                        convertedParams[i] = TypeChanger.Convert(Parameters[i], methodParameters[i].ParameterType);
                    }
                }
            }
            try
            {
                return(obj.GetType().InvokeMember(serviceName, BindingFlags.DeclaredOnly |
                                                  BindingFlags.Public | BindingFlags.NonPublic |
                                                  BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, convertedParams));
            }
            catch
            {
                return(methodInfo.Invoke(obj, convertedParams));
            }
        }
Exemplo n.º 2
0
        public object InvokeMethod(object obj, string MethodName, Type returnType, params object[] Parameters)
        {
            object res = InvokeMethod(obj, MethodName, Parameters);

            if (returnType != typeof(void))
            {
                return(TypeChanger.Convert(res, returnType));
            }
            return(null);
        }
Exemplo n.º 3
0
        public object InvokeMethod(object obj, string MethodName, params object[] Parameters)
        {
            var methodInfo      = obj.GetType().GetMethod(MethodName);
            var parameters      = methodInfo.GetParameters();
            var convertedParams = new object[parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                convertedParams[i] = TypeChanger.Convert(Parameters[i], parameters[i].ParameterType);
            }
            try
            {
                return(obj.GetType().InvokeMember(MethodName, BindingFlags.DeclaredOnly |
                                                  BindingFlags.Public | BindingFlags.NonPublic |
                                                  BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, convertedParams));
            }
            catch
            {
                return(methodInfo.Invoke(obj, convertedParams));
            }
        }