Exemplo n.º 1
0
        private static void AddCustomizedType(BaseProfile profile)
        {
            var customizedTypes = profile.GetAllCustomizedTypes();

            //初始化类型池
            foreach (var customizedType in customizedTypes)
            {
                var customizedTypeCollection = new CustomTypeCollection(customizedType.Name);
                CustomTypeManager.AddCustomizedTypeCollection(customizedType.Name,
                                                              customizedTypeCollection);

                CustomTypeManager.AddCustomizedType(customizedType.Name, customizedType);


                //加到注册表
                ResourceClassRegister.AddCustomizedType(customizedType.Name, customizedType.FullName,
                                                        profile.GetPackageName());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     创建ResourceTemplate
        /// </summary>
        /// <param name="resourceTemplateType"></param>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        /// added by sunjian 2019-12-24
        private static IResourceTemplate CreateTemplateResource(string resourceTemplateType, string resourceName)
        {
            try
            {
                var resourcePath = ResourceClassRegister.GetResourceTemplateFullName(resourceTemplateType);

                var packageName = ResourceClassRegister.GetPackageName(resourceTemplateType);

                var findingPath  = resourcePath + "," + packageName;
                var resourceType = Type.GetType(findingPath, true);

                var obj = Activator.CreateInstance(resourceType, resourceName);

                return((IResourceTemplate)obj);
            }
            catch (Exception ex)
            {
                Log.Error($"创建资源模板:{resourceTemplateType},名称:{resourceName}失败:{ex}");
                return(null);
            }
        }
Exemplo n.º 3
0
        private static IResource CreateResource(string strResourceType, string resourceName, string genericType)
        {
            // 根据类型加载资源 -- Dongmin 20171226
            try
            {
                var resourcePath = ResourceClassRegister.GetResourceFullName(strResourceType);

                var packageName = ResourceClassRegister.GetPackageName(strResourceType);

                var findingPath = resourcePath + "," + packageName;

                var resourceType = Type.GetType(findingPath, true);

                object obj;

                if (string.IsNullOrEmpty(genericType))
                {
                    obj = Activator.CreateInstance(resourceType, resourceName);
                }
                //sunjian 2020-08 增加泛型资源的创建,GenericType必须是实现了ICustomType接口的类
                else
                {
                    var genericObj = Parameter.CreateValue(genericType);

                    var makeGenericType = resourceType.MakeGenericType(genericObj.GetType());

                    obj = Activator.CreateInstance(makeGenericType, resourceName);
                }

                return((IResource)obj);
            }
            catch (Exception ex)
            {
                Log.Error($"创建资源类型:{strResourceType},名称:{resourceName}失败:{ex}");
                return(null);
            }
        }
Exemplo n.º 4
0
        private static void LoadAllPackages()
        {
            //////////////////////////////////////////////
            var localPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            #region // 加载主资源包

            var strResourceLibrary = "ProcessControlService.ResourceLibrary.dll";
            Log.Info($"加载{strResourceLibrary}...");
            var resourceLibrary = new PackageLoader(localPath + strResourceLibrary); //Robin

            if (resourceLibrary.LoadSucceeded)
            {
                //获得包的描述文件
                var profile = resourceLibrary.Profile;

                ///////////////////////////////////////////////////////////////////
                //加载资源

                // 获取包里的所有资源
                var resourceTemplateTypes = profile.GetAllResourceTemplateTypes();

                // 初始化资源池
                foreach (var resourceTemplate in resourceTemplateTypes)
                {
                    ResourceClassRegister.AddResourceTemplate(resourceTemplate.Name, resourceTemplate.FullName,
                                                              profile.GetPackageName());
                }

                ///////////////////////////////////////////////////////////////////
                //加载资源

                // 获取包里的所有资源
                var resourceTypes = profile.GetAllResourceTypes();

                // 初始化资源池
                foreach (var resource in resourceTypes)
                {
                    var resourceClassName = resource.Name.Split('`')[0];

                    try
                    {
                        var resourceCollection = new ResourceColletion(resourceClassName);

                        TypedResourceCollections.Add(resourceClassName, resourceCollection);

                        ResourceClassRegister.AddResource(resourceClassName, resource.FullName, profile.GetPackageName());
                    }
                    catch (Exception e)
                    {
                        Log.Error($"资源:[{resourceClassName}]加载入资源池失败,异常为:{e.Message}.");
                        throw new DuplicateKeyException(resource, $"初始化资源池失败,异常为:{e.Message}.");
                    }
                }

                AddCustomizedType(profile);
            }

            //////////////////////////////////////////////////

            #endregion

            //////////////////////////////////////////////////

            #region // 加载扩展资源包

            var fullPath = localPath + "ExtendResources.xml";
            var doc      = new XmlDocument();
            doc.Load(fullPath);
            var node = (XmlElement)doc.SelectSingleNode("Configs/ResourceModule");
            if (node != null)
            {
                foreach (XmlNode level1Node in node)
                {
                    if (level1Node.NodeType == XmlNodeType.Comment)
                    {
                        continue;
                    }

                    var item = (XmlElement)level1Node;

                    if (Convert.ToBoolean(item.Attributes["Enable"].InnerText))
                    {
                        var fileName = item.Attributes["Name"].InnerText;
                        Log.Info($"加载{fileName}...");
                        var extend1 = new PackageLoader(localPath + fileName); //Robin

                        if (extend1.LoadSucceeded)
                        {
                            var profile = extend1.Profile;

                            ///////////////////////////////////////////////////////////////////
                            //加载资源
                            var resourceTypes = profile.GetAllResourceTypes();

                            //初始化资源池
                            foreach (var resource in resourceTypes)
                            {
                                var resourceCollection = new ResourceColletion(resource.Name);
                                TypedResourceCollections.Add(resource.Name, resourceCollection);

                                //加到注册表
                                ResourceClassRegister.AddResource(resource.Name, resource.FullName,
                                                                  profile.GetPackageName());
                            }

                            ///////////////////////////////////////////////////////////////////
                            //加载自定义类型
                            AddCustomizedType(profile);
                        }
                        //////////////////////////////////////////////////

                        // 把各个资源包加到主资源包
                        resourceLibrary.Profile.AddOtherPackage(extend1.Profile);
                    }
                }
            }

            #endregion

            // 主资源包执行加载附加资源
            resourceLibrary.Profile.LoadAdditionalResource();

            // 显示所有加载的程序集 -- Dongmin 20191015
            Log.Info("已加载程序集:");
            var   loadAssemblies = PackageLoader.GetAllProcessControlServiceAssembly();
            short assIndex       = 0;
            foreach (var assembly in loadAssemblies)
            {
                Log.Info($"程序集{assIndex++}:{assembly}");
            }
        }