Ejemplo n.º 1
0
        /// <summary>
        /// 创建对象实例
        /// </summary>
        /// <param name="targetType"></param>
        /// <param name="typeAlias"></param>
        /// <returns></returns>
        public static object BuildObject(this Type targetType, string typeAlias)
        {
            var objType = GetMapType(targetType, typeAlias);

            if (null == objType)
            {
                throw new ArgumentNullException("targetType", string.Format("Cannot find type : {0}!", typeAlias));
            }
            if (TypeExtension.CanCreated(objType) == false)
            {
                throw new NotImplementedException(string.Format("{0} cannot be created", objType));
            }

            try
            {
                var obj = Activator.CreateInstance(objType, true);

                return(obj);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(string.Format("{0} cannot be created : {1}", objType, ex.Message));
            }
        }