Beispiel #1
0
        public object WCFRESTful(string assemblyName, string className, string method,
                                 List <IRAPJsonTable> jsonTable, out int errCode, out string errText)
        {
            try
            {
                if (!_assemblyList.ContainsKey(className))
                {
                    _assemblyList.Add(className, assemblyName);
                    Loader.LoadAssembly(assemblyName, className);
                }
                Hashtable dict = new Hashtable();
                //组装成为还有复杂类型的Hashtable
                foreach (IRAPJsonTable tableRow in jsonTable)
                {
                    Type type = Type.GetType(tableRow.FullName);
                    if (type == null)
                    {
                        dict.Add(tableRow.Key, tableRow.JsonContent);
                        continue;
                    }

                    object srcObj = IRAPJsonSerializer.Deserializer(tableRow.JsonContent, tableRow.FullName);
                    dict.Add(tableRow.Key, srcObj.ToString());
                }
                object[] obj    = new object[] { dict };
                object   result = Loader.InvokeMethodEx(assemblyName, className, method, obj, out errCode, out errText);

                IRAPJsonResult res = result as IRAPJsonResult;
                //backType = res.TypeName;
                return(res);
            }
            catch (Exception err)
            {
                _assemblyList.Remove(className);
                errCode = 999999;
                errText = string.Format("调用程序集{0}->{1}异常:{2}", assemblyName, method, err.Message + "\n" + err.InnerException.Message);
                //backType = "".GetType().FullName;
                return(new IRAPJsonResult("", "".GetType().FullName));
            }
        }
Beispiel #2
0
        public object ExChange(string assemblyName, string className, string method,
                               List <IRAPJsonTable> jsonTable, out string backType, out int errCode, out string errText)
        {
            try
            {
                if (!_assemblyList.ContainsKey(className))
                {
                    _assemblyList.Add(className, assemblyName);
                    Loader.LoadAssembly(assemblyName, className);
                }
                Hashtable dict = new Hashtable();
                //组装成为还有复杂类型的Hashtable
                foreach (IRAPJsonTable tableRow in jsonTable)
                {
                    object rowObj = IRAPJsonSerializer.Deserializer(tableRow.JsonContent, tableRow.FullName);
                    dict.Add(tableRow.Key, rowObj);
                }
                object[] obj    = new object[] { dict, -1, string.Format("No Runing->{0}->{1}-{2}", assemblyName, className, method) };
                object   result = Loader.InvokeMethod(assemblyName, className, method, obj, out errCode, out errText);
                if (result == null || result.GetType() == null)
                {
                    backType = "".GetType().FullName;
                    return(IRAPJsonSerializer.Serializer(""));
                }
                backType = result.GetType().FullName;

                return(IRAPJsonSerializer.Serializer(result));
            }
            catch (Exception err)
            {
                _assemblyList.Remove(className);
                errCode = 999999;
                //errText = string.Format("调用程序集{0}->{1}异常:{2}", assemblyName, method, err.Message);
                errText  = string.Format("调用程序集{0}->{1}异常:{2}", assemblyName, method, err.Message + "\n" + err.InnerException.Message);
                backType = "".GetType().FullName;
                return("");
            }
        }
Beispiel #3
0
        public object InvokeMethodEx(string methodName, object[] methodParams, out int errCode,
                                     out string errText)
        {
            MethodInfo method;

            if (string.IsNullOrEmpty(methodName))
            {
                throw new Exception("Method Name IsNullOrEmpty");
            }

            _Methods.TryGetValue(methodName, out method);

            if (method == null)
            {
                throw new Exception("Method can not be found");
            }
            Hashtable dict = methodParams[0] as Hashtable;

            //Dictionary<string, string> dict = methodParams[0] as Dictionary<string, string>;
            ParameterInfo[] paramsInfo = method.GetParameters();//得到指定方法的参数列表
            //真正的参数类型
            object[] trueParams   = new object[paramsInfo.Length];
            int      i            = 0;
            int      errCodeIndex = -1;
            int      errTextIndex = -1;

            foreach (ParameterInfo paramItem in paramsInfo)
            {
                if (paramItem.IsOut)
                {
                    if (paramItem.Name == "errCode")
                    {
                        errCodeIndex = i;
                    }
                    if (paramItem.Name == "errText")
                    {
                        errTextIndex = i;
                    }
                    i++;
                    continue;
                }
                Type tType  = paramItem.ParameterType;
                bool isJson = false;
                if (tType.Equals(typeof(string)) || (!tType.IsInterface && !tType.IsClass))
                {
                    isJson = false;
                }
                else if (tType.IsClass)
                {
                    isJson = true;
                }
                foreach (DictionaryEntry item in dict)
                {
                    if (item.Key.ToString() == paramItem.Name)
                    {
                        try
                        {
                            if (!isJson)
                            {
                                trueParams[i] = Convert.ChangeType(item.Value, tType);
                            }
                            else
                            {
                                object targetObj = IRAPJsonSerializer.Deserializer(item.Value.ToString(),
                                                                                   tType);
                                trueParams[i] = Convert.ChangeType(targetObj, tType);
                            }
                        }
                        catch (Exception err)
                        {
                            errCode = 9999;
                            errText = "方法:" + methodName + "中参数:" + paramItem.Name + " 赋值时转换失败:" + err.Message;
                            return(null);
                        }
                        // i++;
                    }
                }
                if (trueParams[i] == null)
                {
                    errCode = 9999;
                    errText = "方法:" + methodName + "中参数:" + paramItem.Name + "没有赋值!不能为NULL哦。";
                    return(null);
                }
                i++;
            }

            object result = tp.InvokeMember(methodName,
                                            BindingFlags.Default | BindingFlags.InvokeMethod, null, _TypeInstance, trueParams);

            errCode = 0;
            errText = "调用" + methodName + "成功!";
            if (errCodeIndex > -1)
            {
                errCode = int.Parse(trueParams[errCodeIndex].ToString());
            }

            if (errTextIndex > -1)
            {
                errText = trueParams[errTextIndex].ToString();
            }
            //这里可以对result进行包装
            return(result);
        }