Exemplo n.º 1
0
        public void Load()
        {
            this.UiTypes.Clear();

            foreach (Type type in this.GetType().Assembly.GetTypes())
            {
                object[] attrs = type.GetCustomAttributes(typeof(UIFactoryAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }

                UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;
                if (UiTypes.ContainsKey(attribute.Type))
                {
                    Log.Debug($"已经存在同类UI Factory: {attribute.Type}");
                    throw new Exception($"已经存在同类UI Factory: {attribute.Type}");
                }
                object     o       = Activator.CreateInstance(type);
                IUIFactory factory = o as IUIFactory;
                if (factory == null)
                {
                    Log.Error($"{o.GetType().FullName} 没有继承 IUIFactory");
                    continue;
                }
                this.UiTypes.Add(attribute.Type, factory);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load初始化 得到所有UI类型
        /// </summary>
        public void Load()
        {
            this.UiTypes.Clear();
            //找到所有有UIFactory特性的类
            List <Type> types = Game.EventSystem.GetTypes(typeof(UIFactoryAttribute));

            //遍历
            foreach (Type type in types)
            {
                //反射获得用户自定义属性
                object[] attrs = type.GetCustomAttributes(typeof(UIFactoryAttribute), false);
                if (attrs.Length == 0)
                {
                    continue;
                }

                UIFactoryAttribute attribute = attrs[0] as UIFactoryAttribute;                 //进行格式转换

                //attribute.Type是自定义的UI类型  用来区分UI

                if (UiTypes.ContainsKey(attribute.Type)) //判断字典中是否已经拥有此UI类型
                {
                    Log.Debug($"已经存在同类UI Factory: {attribute.Type}");
                    throw new Exception($"已经存在同类UI Factory: {attribute.Type}");
                }
                object     o       = Activator.CreateInstance(type);   //实例化
                IUIFactory factory = o as IUIFactory;                  //所有UI类型  都需继承IUIFactory 接口
                if (factory == null)
                {
                    Log.Error($"{o.GetType().FullName} 没有继承 IUIFactory");
                    continue;
                }
                this.UiTypes.Add(attribute.Type, factory);                  //添加到字典中
            }
        }