예제 #1
0
    public static void Main()
    {
        InterfaceImplement ip = new InterfaceImplement();

        ip.InterfaceMethod();
        ((INewInterface2)ip).InterfaceMethod();

        // if a class is inheriting 2 interfaces which have same method names, then we sue explicit interface . so know which method we are calling
        // typecast
        // during explicit implementation, we cannot implement with classname.methodname(); it shouldbe ((interfacename).classname).methodname();

        INewInterface1 i1 = new InterfaceImplement();
        INewInterface2 i2 = new InterfaceImplement();

        i1.InterfaceMethod();
        i2.InterfaceMethod();
    }
        private UnityConfigContainer GetContainerFromXmlNode(XmlNode node)
        {
            //UnityContainerTypeMapper mapper = new UnityContainerTypeMapper();
            InterfaceMapper mapper        = new InterfaceMapper();
            string          containerName = node.Attributes["name"] == null ? "default" : node.Attributes["name"].Value;

            //string baseContainerName = node.Attributes["base"] == null ? string.Empty : node.Attributes["base"].Value;
            //if (!string.IsNullOrEmpty(baseContainerName))
            //{
            //    UnityConfigContainer baseContainer = this.GetContainer(baseContainerName) as UnityConfigContainer;
            //    if (baseContainer == null)
            //        throw new UnityException("没有找到名称为" + baseContainerName + "的反转控制容器");
            //    baseContainer.Mapper.FillMapper(mapper);//从基础容器中填充信息
            //}

            foreach (XmlNode item in node.ChildNodes)
            {
                switch (item.Name)
                {
                case "types":
                {
                    foreach (XmlNode typeNode in item.ChildNodes)
                    {
                        if (typeNode.Name == "add")
                        {
                            string interfaceName = typeNode.Attributes["type"].Value, implementName = typeNode.Attributes["mapTo"].Value;
                            bool   isSingleton          = typeNode.Attributes["isSingleton"] != null && typeNode.Attributes["isSingleton"].Value == "true";
                            UnityResolveArgument[] args = null;
                            XmlNodeList            list = typeNode.SelectNodes("params/add");
                            if (list.Count > 0)
                            {
                                var argsCount = list.Count;
                                args = new UnityResolveArgument[argsCount];
                                for (var i = 0; i < argsCount; i++)
                                {
                                    XmlNode argNode = list[i];
                                    args[i] = new UnityResolveArgument(argNode.Attributes["name"].Value, argNode.Attributes["value"].Value);
                                }
                            }

                            Type impType = Type.GetType(implementName);
                            if (impType == null)
                            {
                                throw new UnityException(string.Format("没有找到 {0} 对应的类型 {1} ", interfaceName, implementName));
                            }

                            Type interfaceType = Type.GetType(interfaceName);
                            if (interfaceType == null)
                            {
                                throw new UnityException(string.Format("没有找到类型 {0} ", interfaceName));
                            }


                            InterfaceImplement impInfo = new InterfaceImplement
                            {
                                ImplementType = impType,
                                IsSingleton   = isSingleton,
                                InitMethod    = (instance) =>
                                {
                                    IUnityResolve resolve = instance as IUnityResolve;
                                    if (resolve != null)
                                    {
                                        resolve.Init(args);
                                    }
                                }
                            };
                            mapper.AddImplement(interfaceType, impInfo);
                        }
                    }
                }
                break;
                }
            }

            return(new UnityConfigContainer(containerName, mapper));
        }