Exemple #1
0
        int ICalculate.Division(int a, int b, AutoCall autoCall)
        {
            int c = 0;

            try
            {
                c = a / b;
            }
            catch (Exception ex)
            {
                autoCall.e(ex.ToString(), System.DJ.ImplementFactory.Commons.ErrorLevels.dangerous);
            }
            return(c);
        }
        public object NewImplement(Type interfaceType, Type implementType, AutoCall autoCall_Impl, bool isShowCode)
        {
            object impl = null;

            string dir = Path.Combine(rootPath, dirName);

            if (!Directory.Exists(dir))
            {
                return(impl);
            }
            DynamicCodeTempImpl dynamicCode = new DynamicCodeTempImpl(dirName);
            string classPath = "";
            string code      = dynamicCode.GetCodeByImpl(interfaceType, implementType, autoCall_Impl, ref classPath);

            if (!string.IsNullOrEmpty(code))
            {
                string fn  = "a" + Guid.NewGuid().ToString().Replace("-", "_");
                string fn1 = fn + ".dll";

                string dllFilePath = Path.Combine(dir, fn1);
                string err         = "";
                if (null == codeCompiler)
                {
                    err = "请提供代码编译器";
                    autoCall_Impl.ExecuteExcption(interfaceType, null, null, null, new Exception(err));
                    return(null);
                }
                Assembly assObj = codeCompiler.TranslateCode(dllArr, "v4.0", code, dllFilePath, ref err);

                if (string.IsNullOrEmpty(err))
                {
                    try
                    {
                        Type type = assObj.GetType(classPath);
                        impl = Activator.CreateInstance(type);
                        instanceCount++;
                    }
                    catch (Exception ex)
                    {
                        err = "加载程序集时出错:\r\n" + ex.ToString();
                    }
                }
                else
                {
                    autoCall_Impl.ExecuteExcption(interfaceType, null, null, null, new Exception(err));
                }

                isShowCode = dynamicCode.IsDataInterface ? IsShowCodeOfDataResourceDLL : isShowCode;

                if (isShowCode)
                {
                    if (!string.IsNullOrEmpty(err))
                    {
                        dllFilePath  = "\r\n/******************\r\n" + err;
                        dllFilePath += "\r\n********/";
                        code        += dllFilePath;
                    }
                    string fn2 = fn + ".cs";
                    string f2  = Path.Combine(dir, fn2);
                    try
                    {
                        File.WriteAllText(f2, code);
                    }
                    catch { }
                }
                else
                {
                    if (!string.IsNullOrEmpty(err))
                    {
                        autoCall_Impl.e(err);
                    }
                }
            }

            autoCall_Impl.CreateInstanceByInterface(interfaceType, impl, autoCall_Impl, instanceCount);

            return(impl);
        }
        public T Exec <T>(MethodInformation method, DataOptType dataOptType, Action <T> action, string sql)
        {
            T        result   = default(T);
            AutoCall autoCall = (AutoCall)method.AutoCall;

            dynamicWhere(method, ref sql);

            IList <DataEntity <DataElement> > dataElements  = null;
            DataEntity <DataElement>          dataElements1 = null;
            DbList <DbParameter> dbParameters = null;

            if (0 < method.paraList.Count)
            {
                foreach (Para item in method.paraList)
                {
                    if (null != item.ParaValue)
                    {
                        if (null != item.ParaValue as IList <DataEntity <DataElement> > )
                        {
                            dataElements = item.ParaValue as IList <DataEntity <DataElement> >;
                            break;
                        }
                        else if (null != item.ParaValue as DataEntity <DataElement> )
                        {
                            dataElements  = new List <DataEntity <DataElement> >();
                            dataElements1 = item.ParaValue as DataEntity <DataElement>;
                            dataElements.Add(dataElements1);
                            break;
                        }
                    }
                }
            }

            if (null == dataElements)
            {
                dataElements = new List <DataEntity <DataElement> >();
            }

            string err           = "";
            bool   EnabledBuffer = method.methodComponent.EnabledBuffer;

            EnabledBuffer = method.methodComponent.IsAsync ? true : EnabledBuffer;

            if (DataOptType.select == dataOptType ||
                DataOptType.count == dataOptType ||
                DataOptType.procedure == dataOptType)
            {
                if (0 < dataElements.Count)
                {
                    dataElements1 = dataElements[0];
                }
                dbParameters = GetDbParameters(method, dataElements1, sql);
                GetSqlByProvider(method, dbParameters, ref sql);
                dbHelper.query(autoCall, sql, dbParameters, EnabledBuffer, (dt) =>
                {
                    DynamicCodeAutoCall dynamicCodeAutoCall = new DynamicCodeAutoCall();
                    string execClassPath  = "";
                    string execMethodName = "";
                    bool paraExsit        = false;
                    Type returnType       = dynamicCodeAutoCall.GetParaTypeOfResultExecMethod(method, ref execClassPath, ref execMethodName, ref paraExsit);
                    if (paraExsit)
                    {
                        Type resultExecMethodType = execClassPath.GetClassTypeByPath();

                        if (null == resultExecMethodType)
                        {
                            err = "The ClassPath '" + execClassPath + "' is not exist.";
                            autoCall.e(err, ErrorLevels.dangerous);
                            throw new Exception(err);
                        }

                        object clsObj         = null;
                        MethodInfo methodInfo = resultExecMethodType.GetMethod(execMethodName);
                        if (null != methodInfo)
                        {
                            if (methodInfo.ReturnType != typeof(T))
                            {
                                err        = "";
                                string msg = "The return value type of the method '{0}' of the class '{1}' and the method '{2}' of the interface '{3}' are not the same.";
                                method.append(ref err, 0, msg, execMethodName, execClassPath, method.methodInfo.Name, method.methodInfo.DeclaringType.FullName);
                                autoCall.e(err, ErrorLevels.dangerous);
                                throw new Exception(err);
                            }

                            object vData = dataTableTo(method, dt, returnType);
                            try
                            {
                                clsObj = Activator.CreateInstance(resultExecMethodType);
                                result = (T)methodInfo.Invoke(clsObj, new object[] { vData });
                            }
                            catch (Exception ex)
                            {
                                autoCall.e(ex.ToString(), ErrorLevels.dangerous);
                                //throw;
                            }
                        }
                    }
                    else
                    {
                        result = dataTableTo <T>(method, dt);
                    }

                    if (null != action)
                    {
                        action(result);
                    }
                }, ref err);
            }
            else
            {
                int           n          = 0;
                Func <int, T> funcResult = n1 =>
                {
                    object v = n1;
                    if (typeof(bool) == typeof(T))
                    {
                        v = 0 < n1;
                    }
                    return((T)v);
                };

                foreach (DataEntity <DataElement> item in dataElements)
                {
                    dbParameters = GetDbParameters(method, item, sql);
                    switch (dataOptType)
                    {
                    case DataOptType.insert:
                        dbHelper.insert(autoCall, sql, dbParameters, EnabledBuffer, (num) =>
                        {
                            n     += num;
                            result = funcResult(n);
                            if (null != action)
                            {
                                action(result);
                            }
                        }, ref err);
                        break;

                    case DataOptType.update:
                        dbHelper.update(autoCall, sql, dbParameters, EnabledBuffer, (num) =>
                        {
                            n     += num;
                            result = funcResult(n);
                            if (null != action)
                            {
                                action(result);
                            }
                        }, ref err);
                        break;

                    case DataOptType.delete:
                        dbHelper.delete(autoCall, sql, dbParameters, EnabledBuffer, (num) =>
                        {
                            n     += num;
                            result = funcResult(n);
                            if (null != action)
                            {
                                action(result);
                            }
                        }, ref err);
                        break;
                    }
                }

                result = funcResult(n);
            }
            return(result);
        }