Exemplo n.º 1
0
        /// <summary>
        /// 初始化程序集信息
        /// </summary>
        private static void InitAssemblyInfo()
        {
            //程序集集合
            GlobalData.MyERPBusinessAssemblyInfos = new ConcurrentBag <MyERPBusinessAssemblyInfo>();
            GlobalData.IOCMapping = new ConcurrentDictionary <string, string>();
            DirectoryInfo directoryInfo = new DirectoryInfo(GlobalData.ERPPath + "/bin");
            var           files         = directoryInfo.GetFiles("*.dll").ToList().FindAll(p => p.Name.StartsWith("Mysoft") && !p.Name.StartsWith("Mysoft.Map"));
            var           totalCount    = files.Count;
            //获取JS源码任务四等分,除不尽五等分,并行执行
            var factorNum  = totalCount / 4;
            var currentNum = 0;

            //任务主体方法
            Action <IEnumerable <FileInfo> > ActionBody = param =>
            {
                foreach (var file in param)
                {
                    ReaderParameters parameters = new ReaderParameters
                    {
                        ReadingMode = ReadingMode.Deferred,
                        ReadSymbols = false,
                    };
                    var module = ModuleDefinition.ReadModule(file.FullName, parameters);
                    MyERPBusinessAssemblyInfo assemblyInfo = new MyERPBusinessAssemblyInfo();
                    assemblyInfo.AssemblyName = module.Name;
                    assemblyInfo.AssemblyPath = file.FullName;
                    var types = module.Types;
                    foreach (var t in types)
                    {
                        if (t.FullName.StartsWith("Mysoft"))
                        {
                            MyERPBusinessAssemblyTypeInfo typeInfo = new MyERPBusinessAssemblyTypeInfo();
                            typeInfo.TypeName     = t.Name;
                            typeInfo.TypeFullName = t.FullName;
                            typeInfo.AssemblyName = assemblyInfo.AssemblyName;
                            typeInfo.AssemblyPath = assemblyInfo.AssemblyPath;

                            var ms  = t.Methods;
                            var prs = t.Fields;
                            foreach (var p in prs)
                            {
                                MyERPBusinessAssemblyFieldInfo fieldInfo = new MyERPBusinessAssemblyFieldInfo();
                                fieldInfo.FieldName     = p.Name;
                                fieldInfo.FieldTypeName = p.FieldType.FullName;
                                typeInfo.Fields.Add(fieldInfo);
                            }

                            foreach (var method in ms)
                            {
                                if (method.Name != ".ctor")
                                {
                                    MyERPBusinessAssemblyMethodInfo methodInfo = new MyERPBusinessAssemblyMethodInfo();
                                    methodInfo.MethodName = method.Name;
                                    methodInfo.IsVirtual  = method.IsVirtual;
                                    methodInfo.IsPublic   = method.IsPublic;
                                    methodInfo.ReturnType = method.ReturnType.FullName;

                                    var ps = method.Parameters;
                                    foreach (var p in ps)
                                    {
                                        MyERPBusinessAssemblyMethodParamInfo paramInfo = new MyERPBusinessAssemblyMethodParamInfo();
                                        paramInfo.ParameterName = p.Name;
                                        paramInfo.ParameterType = p.ParameterType.FullName;
                                        methodInfo.Paramters.Add(paramInfo);
                                    }
                                    typeInfo.Methods.Add(methodInfo);
                                }
                            }
                            assemblyInfo.Types.Add(typeInfo);

                            if (t.Name == "AppInitializer")
                            {
                                //查找IOC的映射配置
                                var initMs = t.Methods;
                                foreach (var method in ms)
                                {
                                    if (method.Name == "Init")
                                    {
                                        if (method.HasBody)
                                        {
                                            Regex  rex = new Regex("(?<MYSTR><.*>)");
                                            string implementTypeFullName = null;
                                            string interfaceTypeFullName = null;
                                            foreach (var item in method.Body.Instructions)
                                            {
                                                if (item.Operand != null)
                                                {
                                                    dynamic temp = item.Operand;
                                                    try
                                                    {
                                                        if (temp.Name == "Register")
                                                        {
                                                            string tempName = temp.FullName;
                                                            tempName = tempName.Substring(tempName.IndexOf("Register"));
                                                            var fullName = rex.Match(tempName).Groups["MYSTR"].ToString().Replace("<", "").Replace(">", "");
                                                            implementTypeFullName = fullName;
                                                        }
                                                        else if (temp.Name == "As")
                                                        {
                                                            string tempName = temp.FullName;
                                                            tempName = tempName.Substring(tempName.IndexOf("As"));
                                                            var fullName = rex.Match(tempName).Groups["MYSTR"].ToString().Replace("<", "").Replace(">", "");
                                                            interfaceTypeFullName = fullName;
                                                            if (implementTypeFullName != null && interfaceTypeFullName != null)
                                                            {
                                                                GlobalData.IOCMapping.TryAdd(interfaceTypeFullName, implementTypeFullName);
                                                                implementTypeFullName = null;
                                                                interfaceTypeFullName = null;
                                                            }
                                                        }
                                                    }
                                                    catch (Exception e)
                                                    {
                                                        //非IOC注入的方法体IL不做处理
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    GlobalData.MyERPBusinessAssemblyInfos.Add(assemblyInfo);
                    module = null;
                }
            };

            for (int i = 0; i < 4; i++)
            {
                var currentList = files.Skip(currentNum * factorNum).Take(factorNum);
                Task.Factory.StartNew(() =>
                {
                    ActionBody(currentList);
                });
                currentNum++;
            }

            var remainder = totalCount % 4;

            if (remainder > 0)
            {
                var currentList = files.Skip(currentNum * factorNum).Take(remainder);
                Task.Factory.StartNew(() =>
                {
                    ActionBody(currentList);
                });
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 初始化程序集信息
        /// </summary>
        private static void InitAssemblyInfo()
        {
            //程序集集合
            GlobalData.MyERPBusinessAssemblyInfos = new System.Collections.Concurrent.ConcurrentBag <MyERPBusinessAssemblyInfo>();
            //PublicService接口和实现类的IOC映射集合
            GlobalData.IOCMappingBody = new ConcurrentBag <Collection <Instruction> >();
            DirectoryInfo directoryInfo = new DirectoryInfo(GlobalData.ERPPath + "/bin");
            var           files         = directoryInfo.GetFiles("*.dll").ToList().FindAll(p => p.Name.StartsWith("Mysoft") && !p.Name.StartsWith("Mysoft.Map"));
            var           totalCount    = files.Count;
            //获取JS源码任务四等分,除不尽五等分,并行执行
            var factorNum  = totalCount / 4;
            var currentNum = 0;

            //任务主体方法
            Action <IEnumerable <FileInfo> > ActionBody = param =>
            {
                foreach (var file in param)
                {
                    ReaderParameters parameters = new ReaderParameters
                    {
                        ReadingMode = ReadingMode.Deferred,
                        ReadSymbols = false,
                    };
                    var module = ModuleDefinition.ReadModule(file.FullName, parameters);
                    MyERPBusinessAssemblyInfo assemblyInfo = new MyERPBusinessAssemblyInfo();
                    assemblyInfo.AssemblyName = module.Name;
                    assemblyInfo.AssemblyPath = file.FullName;
                    var types = module.Types;
                    foreach (var t in types)
                    {
                        if (t.FullName.StartsWith("Mysoft"))
                        {
                            MyERPBusinessAssemblyTypeInfo typeInfo = new MyERPBusinessAssemblyTypeInfo();
                            typeInfo.TypeName     = t.Name;
                            typeInfo.TypeFullName = t.FullName;
                            typeInfo.AssemblyName = assemblyInfo.AssemblyName;
                            typeInfo.AssemblyPath = assemblyInfo.AssemblyPath;

                            var ms  = t.Methods;
                            var prs = t.Fields;
                            foreach (var p in prs)
                            {
                                MyERPBusinessAssemblyFieldInfo fieldInfo = new MyERPBusinessAssemblyFieldInfo();
                                fieldInfo.FieldName     = p.Name;
                                fieldInfo.FieldTypeName = p.FieldType.FullName;
                                typeInfo.Fields.Add(fieldInfo);
                            }

                            foreach (var method in ms)
                            {
                                if (method.Name != ".ctor")
                                {
                                    MyERPBusinessAssemblyMethodInfo methodInfo = new MyERPBusinessAssemblyMethodInfo();
                                    methodInfo.MethodName = method.Name;
                                    methodInfo.IsVirtual  = method.IsVirtual;
                                    methodInfo.IsPublic   = method.IsPublic;
                                    methodInfo.ReturnType = method.ReturnType.FullName;

                                    var ps = method.Parameters;
                                    foreach (var p in ps)
                                    {
                                        MyERPBusinessAssemblyMethodParamInfo paramInfo = new MyERPBusinessAssemblyMethodParamInfo();
                                        paramInfo.ParameterName = p.Name;
                                        paramInfo.ParameterType = p.ParameterType.FullName;
                                        methodInfo.Paramters.Add(paramInfo);
                                    }
                                    typeInfo.Methods.Add(methodInfo);
                                }
                            }
                            assemblyInfo.Types.Add(typeInfo);

                            if (t.Name == "AppInitializer")
                            {
                                //查找IOC的映射配置
                                var initMs = t.Methods;
                                foreach (var method in ms)
                                {
                                    if (method.Name == "Init")
                                    {
                                        if (method.HasBody)
                                        {
                                            GlobalData.IOCMappingBody.Add(method.Body.Instructions);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    GlobalData.MyERPBusinessAssemblyInfos.Add(assemblyInfo);
                    module = null;
                }
            };

            for (int i = 0; i < 4; i++)
            {
                var currentList = files.Skip(currentNum * factorNum).Take(factorNum);
                Task.Factory.StartNew(() =>
                {
                    ActionBody(currentList);
                });
                currentNum++;
            }

            var remainder = totalCount % 4;

            if (remainder > 0)
            {
                var currentList = files.Skip(currentNum * factorNum).Take(remainder);
                Task.Factory.StartNew(() =>
                {
                    ActionBody(currentList);
                });
            }
        }