예제 #1
0
 private bool IsTypeRegisterdMethod(Type from, string name)
 {
     lock (thislock)
     {
         if (name == null)
         {
             return(typeMapping.ContainsKey(from) || ObjectMapping.ContainsKey(from) || singletonMapping.ContainsKey(from));
         }
         else
         {
             CustomKey key = new CustomKey(from, name);
             return(typeNameMapping.ContainsKey(key) || ObjectNameMapping.ContainsKey(key));
         }
     }
 }
예제 #2
0
        private void RegisterInstanceMethod(Type from, object instance, string name)
        {
            Type value;

            lock (thislock)
            {
                if (name == null)
                {
                    ObjectMapping[from] = instance;

                    typeMapping.TryRemove(from, out value);

                    singletonMapping.TryRemove(from, out value);
                }
                else
                {
                    CustomKey key = new CustomKey(from, name);
                    ObjectNameMapping[key] = instance;

                    typeNameMapping.TryRemove(key, out value);
                }
            }
        }
예제 #3
0
        private void RegisterTypeMethod(Type from, Type to, string name)
        {
            object objectValue;
            Type   keyValue;

            lock (thislock)
            {
                if (name == null)
                {
                    typeMapping[from] = to;

                    ObjectMapping.TryRemove(from, out objectValue);

                    singletonMapping.TryRemove(from, out keyValue);
                }
                else
                {
                    CustomKey key = new CustomKey(from, name);
                    typeNameMapping[key] = to;

                    ObjectNameMapping.TryRemove(key, out objectValue);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// 解析函数 会递归调用解析构造参数;
        /// 对于不在映射表里的,如果是可实例化的类型也能解析
        /// 对于内置类型,如果支持的话,构造为默认值
        /// </summary>
        /// <param name="Type">类型</param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        private object DoResolve(Type @Type, string name = null)
        {
            bool   isMapping = false;
            Type   type;
            object instance;

            if (name == null)
            {
                if (typeMapping.TryGetValue(@Type, out type))
                {
                    isMapping = true;
                }

                else if (ObjectMapping.TryGetValue(@Type, out instance))
                {
                    isMapping = true;
                    return(instance);
                }
                else if (singletonMapping.TryGetValue(@Type, out type))//第一次进入
                {
                    isMapping = true;
                }
                else
                {
                    type      = @Type;
                    isMapping = false;
                }
            }
            else
            {
                CustomKey key = new CustomKey(@Type, name);

                if (typeNameMapping.TryGetValue(key, out type))
                {
                    isMapping = true;
                }
                else if (ObjectNameMapping.TryGetValue(key, out instance))
                {
                    isMapping = true;
                    return(instance);
                }
                else
                {
                    if (typeNameMapping.ContainsKey(key))
                    {
                    }
                    type      = @Type;
                    isMapping = false;
                }
            }

            if (!isMapping && !isSupportBuildType && IsBulitinType(@Type))
            {
                throw new TargetException($"当前{@Type.Name}不支持,可通过设置isSupportBuildType支持");
            }

            if (type.IsInterface)
            {
                throw new TargetException($"当前{type.Name}是个接口,不能被构造,是否缺少对照");
            }
            else if (type.IsAbstract)
            {
                throw new TargetException($"当前{type.Name}是个抽象类,不能被构造,是否缺少对照");
            }


            //通过反射,获取对象的构造函数
            ConstructorInfo constructor = GetConstructor(type);

            if (null == constructor)
            {
                return(null);
            }

            // 获取构造函数的参数,以及使用 递归得到实参来初始化
            object[] arguments = constructor.GetParameters().Select(p => this.DoResolve(p.ParameterType)).ToArray();

            //获得构造函数创造的对象
            object service = constructor.Invoke(arguments);

            return(service);
        }