Beispiel #1
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 #2
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());
            }
        }