Activator.CreateInstance

   private Dictionary m_ObjectType = new Dictionary();


   public T CreateNewInstance() where T : class
        {
            if (m_ObjectType.ContainsKey(typeof(T)))
            {
                Type insType = m_ObjectType[typeof(T)];
                return Activator.CreateInstance(insType) as T;
            }
            else
            {
                throw new Exception("未知的类型"+ typeof(T).ToString());
            }
        }

        public T CreateNewInstance(params object[] args) where T:class
        {
            if (m_ObjectType.ContainsKey(typeof(T)))
            {
                Type insType = m_ObjectType[typeof(T)];
                return Activator.CreateInstance(insType, args) as T;
            }
            else
            {
                throw new Exception("未知的类型" + typeof(T).ToString());
            }
        }

   public IDatabaseItem CreateNewDatabaseItem(string name, string description, 
    DatabaseItemType tt, IDatabaseItem parent)
        {
            switch (tt)
            {
                case DatabaseItemType.Database:
                    return CreateNewInstance(name, parent);
                case DatabaseItemType.Field:
                    return CreateNewInstance(name, description, 
                      parent);
                case DatabaseItemType.Server:
                    return CreateNewInstance(name);
                case DatabaseItemType.Table:
                    return CreateNewInstance(name, parent);
                default:
                    throw new Exception("未知的数据库类型");
            }
        }

  

  •  

你可能感兴趣的:(C#,bootstrap)