Exemple #1
0
        /// <summary>
        /// 注册单例对象
        /// </summary>
        /// <typeparam name="T">接口</typeparam>
        /// <typeparam name="V">实现</typeparam>
        /// <param name="obj">已实例化对象</param>
        /// <param name="name">名字</param>
        public static void RegistSingleton <T, V>(V obj, string name) where V : class, T, new()
        {
            SingletonTypeNode node = null;

            if (obj != null)
            {
                node = new SingletonTypeNode(obj);
            }
            else
            {
                node = new SingletonTypeNode(typeof(V));
            }

            Regist <T>(name, node);
        }
Exemple #2
0
        /// <summary>
        /// 解析对象
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="name">名字</param>
        /// <returns>对应实例化对象</returns>
        public static object Resolve(Type type, string name = null)
        {
            object res = null;

            name = string.IsNullOrEmpty(name) ? string.Empty : name;

            Dictionary <string, ITypeNode> dic = null;

            if (_dic.TryGetValue(type, out dic))
            {
                ITypeNode node = null;

                dic?.TryGetValue(name, out node);

                if (node is NormalTypeNode)
                {
                    NormalTypeNode norNode = node as NormalTypeNode;

                    res = Activator.CreateInstance(norNode.objType);

                    GenerateInterfaceField(res);
                }
                else if (node is SingletonTypeNode)
                {
                    SingletonTypeNode singleNode = node as SingletonTypeNode;

                    res = singleNode.Obj;
                }
            }

            if (res == null)
            {
                Debug.LogErrorFormat("<Ming> ## Uni Error ## Cls:Container Func:Resolve Type:{0}{1} Info:Unregistered", type, !string.IsNullOrEmpty(name) ? $" Name:{name}" : string.Empty);
            }

            return(res);
        }
Exemple #3
0
        /// <summary>
        /// 注册单例对象
        /// </summary>
        /// <typeparam name="T">接口</typeparam>
        /// <typeparam name="V">实现</typeparam>
        public static void RegistSingleton <T, V>() where V : class, T, new()
        {
            SingletonTypeNode node = new SingletonTypeNode(typeof(V));

            Regist <T>(string.Empty, node);
        }