NET 判断类型是否为可空类型

可控类型本质上是泛型,所以可以先判断是否为泛型,在判断是否为:Nullable

        /// <summary>
        /// 是否为 可空类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns>true ? 是-可空类型 : 不是-可空类型</returns>
        public static bool IsNullableType(this Type type)
        {
            //  可空类型的类型为:Nullable<int/long...>
            if (type.IsGenericType)
            {
                var definition = type.GetGenericTypeDefinition();

                if (definition != null && definition == typeof(Nullable<>))
                {
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }

 

posted @ 2022-06-17 15:42  Robot-Blog  阅读(197)  评论(0编辑  收藏  举报