alexmen

专注.net软件开发,项目管理体系PMBOK.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
/// <summary>
        /// 创建实例 支持泛型的创建  
        /// </summary>
        /// <typeparam name="T">此处T形如 Common<User></typeparam>
        /// <returns></returns>
        public static T Create<T>()
        {
            var t = typeof (T);
            if (t.IsInterface)
            {
                var name = string .Format("{0}.{1}", AssemblyStr, t.Name.TrimStart('I' ));
                var type = GetAssembly().GetType(name);
                if (type.IsGenericType)
                {
                    var typeArgument = t.GetGenericArguments().FirstOrDefault();  //单个泛型形参
                    var genericType = type.MakeGenericType(typeArgument);    
                    object O = (T)Activator .CreateInstance(genericType);
                }
                else
                {
                    return (T)Activator .CreateInstance(type);
                }
 
 
            }
            return default (T);
        }
posted on 2016-08-02 23:01  alexmen  阅读(285)  评论(0编辑  收藏  举报