コード例 #1
0
        static void init()
        {
            if (string.IsNullOrEmpty(rootPath))
            {
                return;
            }

            matchRules = MatchRules();

            TempImpl tempImpl = new TempImpl();

            tempImpl.SetRootPath(rootPath);
            tempImpl.IsShowCodeOfDataResourceDLL = dbInfo.IsShowCode;

            DataAdapter.SetConfig(dbInfo.ConnectionString, dbInfo.DatabaseType);

            assemblies = DJTools.GetAssemblyCollection(rootPath);
            AutoCall.AssemblyCollection = assemblies;
            AutoCall.SetDataProviderAssemble(dbInfo.SqlProviderRelativePathOfDll);
            AutoCall.RootPath = rootPath;
            AutoCall.GetDllAbsolutePathByRelativePath = GetDllAbsolutePathByRelativePath;

            DbList <Data.Common.DbParameter> .dataServerProvider = dataServerProvider;

            if (null != DbHelper)
            {
                DbHelper.connectString        = dbInfo.ConnectionString;
                DbHelper.optByBatchMaxNumber  = dbInfo.optByBatchMaxNumber;
                DbHelper.optByBatchWaitSecond = dbInfo.optByBatchWaitSecond;
                DbHelper.sqlMaxLengthForBatch = dbInfo.sqlMaxLengthForBatch;
                DbHelper.disposableAndClose   = dbInfo.close;
            }
        }
コード例 #2
0
        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);
                }
            }
        }