예제 #1
0
        private CastleFactory()
        {
            var assembly2 = Assembly.LoadFrom("MySoft.IoC.dll");

            this.factoryType = assembly2.GetType("MySoft.IoC.CastleFactory");
            this.invokeType  = assembly2.GetType("MySoft.IoC.Messages.InvokeMessage");
            this.valueType   = assembly2.GetType("MySoft.IoC.Messages.InvokeData");

            this.assembly    = Assembly.LoadFrom("MySoft.Core.dll");
            this.managerType = assembly.GetType("MySoft.SerializationManager");

            //创建castle对象
            var method = factoryType.GetMethod("Create");

            this.instance = DynamicCalls.GetMethodInvoker(method).Invoke(null, null);
        }
예제 #2
0
        /// <summary>
        /// 服务调用
        /// </summary>
        /// <param name="proxy"></param>
        /// <param name="method"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object Invoke(object proxy, MethodInfo method, object[] parameters)
        {
            try
            {
                var invoke = DynamicCalls.GetInstanceCreator(invokeType)();
                DynamicCalls.GetPropertySetter(serviceProperty).Invoke(invoke, serviceType.FullName);
                DynamicCalls.GetPropertySetter(methodProperty).Invoke(invoke, method.ToString());

                var dictIndex     = new Dictionary <string, InvokeResult>();
                var dictParameter = new Hashtable();
                var index         = 0;
                foreach (var p in method.GetParameters())
                {
                    if (p.IsOut || p.ParameterType.IsByRef)
                    {
                        dictIndex[p.Name] = new InvokeResult {
                            Index = index, ResultType = GetElementType(p.ParameterType)
                        };
                        if (p.IsOut)
                        {
                            index++;
                            continue;
                        }
                    }

                    dictParameter[p.Name] = parameters[index];
                    index++;
                }
                string paramString = null;
                if (dictParameter.Count > 0)
                {
                    paramString = (string)DynamicCalls.GetMethodInvoker(serializeMethod).Invoke(null, new object[] { dictParameter, null });
                }

                DynamicCalls.GetPropertySetter(parametersProperty).Invoke(invoke, paramString);

                object value = null;
                if (string.IsNullOrEmpty(nodeKey))
                {
                    value = DynamicCalls.GetMethodInvoker(invokeMethod).Invoke(instance, new object[] { invoke });
                }
                else
                {
                    value = DynamicCalls.GetMethodInvoker(invokeMethod).Invoke(instance, new object[] { nodeKey, invoke });
                }

                var json     = DynamicCalls.GetPropertyGetter(valueProperty).Invoke(value);
                var retValue = DynamicCalls.GetMethodInvoker(deserializeMethod).Invoke(null, new object[] { GetElementType(method.ReturnType), json, null });

                if (dictIndex.Count > 0)
                {
                    var outJson = DynamicCalls.GetPropertyGetter(outProperty).Invoke(value);
                    var dictOut = DynamicCalls.GetMethodInvoker(deserializeMethod).Invoke(null, new object[] { typeof(Hashtable), outJson, null });

                    //返回参数赋值
                    dictParameter = dictOut as Hashtable;
                    foreach (var kv in dictIndex)
                    {
                        var    result   = kv.Value;
                        object outValue = DynamicCalls.GetMethodInvoker(deserializeMethod)
                                          .Invoke(null, new object[] { result.ResultType, dictParameter[kv.Key].ToString(), null });

                        parameters[result.Index] = outValue;
                    }
                }

                return(retValue);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    throw ex.InnerException;
                }
                else
                {
                    throw ex;
                }
            }
        }