Пример #1
0
 /// <summary>
 /// 注册抽象类型需要使用的实体类型
 /// 该类型实体具有构造参数,实际的配置信息可以从外层机制获得。
 /// </summary>
 /// <param name="type"></param>
 /// <param name="typeConstructor"></param>
 public void AddTypeConstructor(Type type, TypeConstructor typeConstructor)
 {
     if (!dictionary.ContainsKey(type))
     {
         dictionary.Add(type, typeConstructor);
     }
     else
     {
         throw new MySoftException(string.Format("类型{0}注入重复!", type.FullName));
     }
 }
Пример #2
0
        /// <summary>
        /// 创建一个实例
        /// </summary>
        /// <returns></returns>
        public T Create()
        {
            TypeConstructor constructor = map[typeof(T)];

            if (constructor != null)
            {
                if (constructor.ConstructorParameters == null)
                {
                    return((T)Activator.CreateInstance(constructor.Type));
                }
                else
                {
                    return((T)Activator.CreateInstance(
                               constructor.Type, constructor.ConstructorParameters));
                }
            }
            else
            {
                return(default(T));
            }
        }