Beispiel #1
0
        private static object[] GetArgs(object[] args, Assembly asy, MethodInfo me, out object[] args2)
        {
            if (args == null)
            {
                args2 = null;
                return(null);
            }
            List <object> list  = new List <object>(args.Length * 2);
            List <object> list2 = new List <object>(args.Length * 2);

            ParameterInfo[] pArray = me.GetParameters();
            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] != null)
                {
                    object x = SoapEntityMapping.ConvertToSoapObject(args[i], asy);
                    list.Add(x);
                    list2.Add(x);
                    Type t = args[i].GetType();
                    if (t.IsValueType)
                    {
                        if (t.IsNullableType())
                        {
                            list.Add(args[i] != null);
                        }
                        else
                        {
                            list.Add(true);
                        }
                    }
                }
                else
                {
                    int len1 = list.Count;
                    int len2 = list2.Count;
                    if (pArray.Length <= len1 || pArray.Length <= len2)
                    {
                        throw new ApplicationException("WebService方法的入参数量不正确");
                    }
                    Type t = pArray[len1].ParameterType;
                    list.Add(DataConvertor.GetValueByType(t, null, null, null));
                    if (t.IsValueType)
                    {
                        list.Add(false);
                    }
                    t = pArray[len2].ParameterType;
                    list2.Add(DataConvertor.GetValueByType(t, null, null, null));
                }
            }
            args2 = list2.ToArray();
            return(list.ToArray());
        }
Beispiel #2
0
        public static T Invoke <T>(string url, string methodName, params object[] args)
        {
            object     proxy = CreateServiceProxyInstance(url);
            MethodInfo me    = proxy.GetType().GetMethod(methodName);

            object[] realArgs2;
            object[] realArgs = GetArgs(args, proxy.GetType().Assembly, me, out realArgs2);
            if (typeof(T).IsValueType)
            {
                try
                {
                    T             obj      = default(T);
                    bool          hasValue = false;
                    List <object> list     = new List <object>(realArgs);
                    list.Add(obj);
                    list.Add(hasValue);
                    object[] x = list.ToArray();
                    me.Invoke(proxy, x);
                    hasValue = (bool)x[x.Length - 1];
                    if (hasValue)
                    {
                        object tmp = x[x.Length - 2];
                        return((T)SoapEntityMapping.ConvertFromSoapObject(tmp, typeof(T)));
                    }
                    else
                    {
                        return(default(T));
                    }
                }
                catch
                {
                    object tmp = Invoker.MethodInvoke(proxy, methodName, realArgs2);
                    return((T)SoapEntityMapping.ConvertFromSoapObject(tmp, typeof(T)));
                }
            }
            else
            {
                object tmp;
                try
                {
                    tmp = Invoker.MethodInvoke(proxy, methodName, realArgs);
                }
                catch
                {
                    tmp = Invoker.MethodInvoke(proxy, methodName, realArgs2);
                }
                return((T)SoapEntityMapping.ConvertFromSoapObject(tmp, typeof(T)));
            }
        }
Beispiel #3
0
        public static void InvokeAsync <T>(string url, string methodName, Action <T> callback, Action <Exception> errorHandler, params object[] args)
        {
            object     proxy           = CreateServiceProxyInstance(url);
            string     beginMethodName = "Begin" + methodName;
            string     endMethodName   = "End" + methodName;
            Type       t           = proxy.GetType();
            MethodInfo beginMethod = t.GetMethod("Begin" + methodName);
            MethodInfo endMethod   = t.GetMethod(endMethodName);

            object[] realArgs2;
            object[] realArgs = GetArgs(args, proxy.GetType().Assembly, beginMethod, out realArgs2);
            //MethodInfo beginMethod = t.GetMethod("Begin" + methodName);
            AsyncCallback cb = ar =>
            {
                T    obj      = default(T);
                bool hasError = false;
                try
                {
                    if (typeof(T).IsValueType)
                    {
                        try
                        {
                            bool     hasValue = false;
                            object[] args1    = new object[] { ar, obj, hasValue };
                            endMethod.Invoke(ar.AsyncState, args1);
                            hasValue = (bool)args1[2];
                            if (hasValue)
                            {
                                obj = (T)SoapEntityMapping.ConvertFromSoapObject(args1[1], typeof(T));
                            }
                            else
                            {
                                obj = default(T);
                            }
                        }
                        catch
                        {
                            object[] args1 = new object[] { ar, obj };
                            endMethod.Invoke(ar.AsyncState, args1);
                            obj = (T)SoapEntityMapping.ConvertFromSoapObject(args1[1], typeof(T));
                        }
                    }
                    else
                    {
                        object tmp = Invoker.MethodInvoke(ar.AsyncState, endMethodName, ar);
                        obj = (T)SoapEntityMapping.ConvertFromSoapObject(tmp, typeof(T));
                    }
                }
                catch (Exception ex)
                {
                    hasError = true;
                    if (errorHandler != null)
                    {
                        errorHandler(ex);
                    }
                }
                if (hasError == false && callback != null)
                {
                    callback(obj);
                }
            };

            try
            {
                List <object> list = new List <object>(realArgs);
                list.Add(cb);
                list.Add(proxy);
                Invoker.MethodInvoke(proxy, beginMethodName, list.ToArray());
            }
            catch
            {
                List <object> list = new List <object>(realArgs2);
                list.Add(cb);
                list.Add(proxy);
                Invoker.MethodInvoke(proxy, beginMethodName, list.ToArray());
            }
        }