Exemple #1
0
        public void Exec(AutoCall autoCall, string sql, List <DbParameter> parameters, ref string err, Action <object> action, Func <DbCommand, object> func)
        {
            lock (_execObj)
            {
                if (DbConnct(ref err))
                {
                    DbCommand cmd = dataServerProvider.CreateDbCommand(sql, conn);
                    if (null != parameters)
                    {
                        foreach (var item in parameters)
                        {
                            cmd.Parameters.Add(item);
                        }
                    }

                    object vObj = null;
                    try
                    {
                        vObj = func(cmd);
                    }
                    catch (Exception ex)
                    {
                        conn.Close();
                        conn.Dispose();
                        conn = null;

                        err  = ex.ToString();
                        err += "\r\n\r\n" + sql;
                        if (null != autoCall)
                        {
                            autoCall.ExecuteExcption(this.GetType(), this, "Exec", null, new Exception(err));
                        }
                        //throw;
                    }
                    finally
                    {
                        cmd.Dispose();
                        if (null != conn)
                        {
                            if (disposableAndClose)
                            {
                                conn.Close();
                            }
                            conn.Dispose();
                        }

                        action(vObj);
                    }
                }
            }
        }
        Type LoadImplementTypeByMatchRule(MatchRule mr, Type interfaceType, AutoCall autoCall)
        {
            Type     impl_type             = null;
            Assembly objAss                = null;
            string   err                   = "";
            string   DllRelativePathOfImpl = mr.DllRelativePathOfImpl;

            if (!string.IsNullOrEmpty(DllRelativePathOfImpl))
            {
                string dllFile = GetDllAbsolutePathByRelativePath(DllRelativePathOfImpl);
                if (!File.Exists(dllFile))
                {
                    return(null);
                }

                try
                {
                    objAss = Assembly.LoadFrom(dllFile);
                }
                catch (Exception ex)
                {
                    err = "加载程序集[" + dllFile + "]出错\r\n" + ex.ToString();
                    autoCall.ExecuteExcption(interfaceType, null, null, null, new Exception(err));
                }
            }
            else
            {
                objAss = interfaceType.Assembly; //Assembly.GetExecutingAssembly(); //当前应用程序集
            }

            if (null == objAss)
            {
                err = "加载程序集失败";
                autoCall.ExecuteExcption(interfaceType, null, null, null, new Exception(err));
                return(impl_type);
            }

            Regex  rg1          = null;
            string mie          = mr.MatchImplExpression;
            bool   isIgnoreCase = mr.IgnoreCase;

            AutoCallMatch(autoCall, ref mie, ref rg1, ref isIgnoreCase);

            if ((!string.IsNullOrEmpty(mr.ImplementNameSpace)) && null == rg1)
            {
                string s    = mr.ImplementNameSpace + "." + mie;
                Type   type = null;
                try
                {
                    type = objAss.GetType(s);
                }
                catch { }
                if (null == type)
                {
                    return(impl_type);
                }
                if (type.IsImplementInterface(interfaceType))
                {
                    impl_type = type;
                }

                return(impl_type);
            }

            string fn1 = isIgnoreCase ? mie.ToLower() : mie;

            Type[] types = null;
            try
            {
                types = objAss.GetTypes();
            }
            catch { }

            impl_type = GetImplTypeByTypes(types, interfaceType, autoCall, rg1, fn1, isIgnoreCase);

            return(impl_type);
        }
        void Adapter()
        {
            if (string.IsNullOrEmpty(rootPath))
            {
                return;
            }

            object[] arr = null;

            string    resetKeyName  = "";
            Type      interfaceType = null;
            Type      implType      = null;
            CKeyValue kv            = null;
            MatchRule mr            = null;
            object    impl          = null;
            object    implNew       = null;
            object    impl_1        = null;

            bool        isShowCode         = false;
            bool        isSingleCall       = false;
            bool        isSingleInstance   = false;
            bool        isUnSingleInstance = false;
            InstanceObj instanceObj        = null;

            string unSingleInstanceStr = typeof(IUnSingleInstance).FullName;

            TempImpl temp = new TempImpl();

            temp.codeCompiler = codeCompiler;

            bool enableCompiler = false;

            if (null != codeCompiler && null != dataServerProvider && null != DbHelper)
            {
                enableCompiler = true;
            }

            Action <object, Action> action = (obj, action1) =>
            {
                if (null != obj)
                {
                    if (null != (obj as IUnSingleInstance))
                    {
                        isUnSingleInstance = true;
                        isSingleCall       = false;
                        isSingleInstance   = false;
                    }

                    if ((null != (obj as ISingleInstance) || isSingleCall) && false == isUnSingleInstance)
                    {
                        isSingleCall     = true;
                        isSingleInstance = true;
                        action1?.Invoke();
                    }
                }
            };

            Regex  rg           = null;
            string implName     = "";
            bool   isIgnoreCase = false;

            AutoCall autoCall = null;

            FieldInfo[] fArr = null; // currentObj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);

            List <Type> objList  = new List <Type>();
            Type        _objType = currentObj.GetType();
            int         n        = 0;

            while (20 > n && typeof(object) != _objType && typeof(ImplementAdapter) != _objType)
            {
                objList.Add(_objType);
                _objType = _objType.BaseType;
                n++;
            }

            if (0 == objList.Count && null != currentObj)
            {
                objList.Add(currentObj.GetType());
            }

            foreach (Type typeItem in objList)
            {
                fArr = typeItem.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
                foreach (FieldInfo p in fArr)
                {
                    arr = p.GetCustomAttributes(typeof(AutoCall), true);
                    if (0 == arr.Length)
                    {
                        continue;
                    }

                    autoCall = null;
                    foreach (var item in arr)
                    {
                        if (null != (item as AutoCall))
                        {
                            autoCall = (AutoCall)item;
                            break;
                        }
                    }

                    interfaceType = p.FieldType;

                    if (!autoCall.LoadBeforeFilter(interfaceType))
                    {
                        continue;
                    }
                    autoCall.errorLevels1 = errorLevels1;

                    impl               = null;
                    impl_1             = null;
                    implType           = null;
                    resetKeyName       = null;
                    isSingleInstance   = false;
                    isUnSingleInstance = false;
                    instanceObj        = null;

                    arr          = p.GetCustomAttributes(typeof(SingleCall), true);
                    isSingleCall = 0 < arr.Length;

                    resetKeyName = GetInterfaceName(interfaceType, autoCall);
                    interfaceImplements.TryGetValue(resetKeyName, out instanceObj);
                    if (null != instanceObj)
                    {
                        impl = instanceObj.newInstance;
                    }
                    if (null != instanceObj)
                    {
                        implName     = "";
                        rg           = null;
                        isIgnoreCase = false;
                        AutoCallMatch(autoCall, ref implName, ref rg, ref isIgnoreCase);
                        if (null != rg)
                        {
                            if (!MatchImpl(rg, instanceObj.oldInstanceType, implName, isIgnoreCase))
                            {
                                impl = null;
                            }
                        }
                    }
                    impl_1 = impl;

                    action(impl, () =>
                    {
                        impl = null;
                    });

                    if (null == impl)
                    {
                        kv         = GetKvByInterfaceType(interfaceType);
                        mr         = null == kv ? null : ((MatchRule)kv.Value);
                        isShowCode = false;
                        if (interfaceType.IsInterface)
                        {
                            if (null != mr)
                            {
                                isShowCode = mr.IsShowCode;
                                implType   = LoadImplementTypeByMatchRule(mr, interfaceType, autoCall);
                            }
                            else
                            {
                                implType = LoadImplementTypeByInterface(interfaceType, autoCall);
                            }

                            if (null == implType)
                            {
                                implType = LoadImplementTypeByAssemblies(interfaceType, autoCall);
                            }

                            if (enableCompiler && null == (autoCall as ExistCall))
                            {
                                implNew = temp.NewImplement(interfaceType, implType, autoCall, isShowCode);
                                if (null != implNew)
                                {
                                    impl = implNew;
                                }
                            }

                            if (null != implType && null == impl)
                            {
                                try
                                {
                                    impl = Activator.CreateInstance(implType);
                                }
                                catch (Exception ex)
                                {
                                    string err = "[" + implType.FullName + "] 实例可能缺少一个无参构造函数(或该类访问权限不够)\r\n" + ex.ToString();
                                    autoCall.ExecuteExcption(interfaceType, null, null, null, new Exception(err));
                                    //throw;
                                }
                            }
                        }
                        else
                        {
                            if (enableCompiler)
                            {
                                implType      = interfaceType;
                                interfaceType = typeof(IEmplyInterface);
                                implNew       = temp.NewImplement(interfaceType, implType, autoCall, isShowCode);
                                if (null != implNew)
                                {
                                    impl = implNew;
                                }
                            }

                            if (null == impl && null != implType)
                            {
                                try
                                {
                                    impl = Activator.CreateInstance(implType);
                                }
                                catch (Exception ex)
                                {
                                    string err = "[" + implType.FullName + "] 实例可能缺少一个无参构造函数(或该类访问权限不够)\r\n" + ex.ToString();
                                    autoCall.ExecuteExcption(interfaceType, null, null, null, new Exception(err));
                                    //throw;
                                }
                            }
                        }

                        if (false == isUnSingleInstance)
                        {
                            action(impl, null);
                        }

                        if (null != impl && null == impl_1 && false == isSingleCall)
                        {
                            lock (_adapterOfImplement)
                            {
                                interfaceImplements.TryGetValue(resetKeyName, out instanceObj);
                                if (null == instanceObj)
                                {
                                    interfaceImplements.Add(resetKeyName, new InstanceObj()
                                    {
                                        newInstance     = impl,
                                        oldInstanceType = implType
                                    });
                                }
                                else
                                {
                                    impl = instanceObj.newInstance;
                                }
                            }
                        }
                    }

                    if (null == impl)
                    {
                        continue;
                    }

                    isSingleInstance = false;
                    if (null != (impl as ISingleInstance))
                    {
                        isSingleInstance = true;
                    }

                    if (isSingleInstance)
                    {
                        if (null == ((ISingleInstance)impl).Instance)
                        {
                            ((ISingleInstance)impl).Instance = impl;
                        }
                    }

                    if (!autoCall.LoadAfterFilter(impl))
                    {
                        continue;
                    }

                    p.SetValue(currentObj, impl);
                }
            }
        }
        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);
        }