コード例 #1
0
ファイル: TypeHelper.cs プロジェクト: luozhiping1987/api
        internal static Type EnsureGenericRuntimeType(Type p)
        {
            Type t = EnsureBoxedType(p);

            if (t != p)
            {
                return(t);
            }

            return(NullableReflection.GetUnderlyingTypeForMarked(p)
                   ?? GenericInstanceFactory.GetGenericTypeDefinition(p)
                   ?? p);
        }
コード例 #2
0
        /// <summary>
        /// Create an instance of the given type using the best matching constructor.
        /// </summary>
        public static object CreateInstance(Type type, params object[] args)
        {
            if (NullableReflection.TreatAsSystemNullableT(type))
            {
                return(null);
            }

            var genericInstance = GenericInstanceFactory.CreateGenericInstance(type, args);

            if (genericInstance != null)
            {
                return(genericInstance);
            }

            var constructor = GetBestMatchingConstructor(type, args);

            return(constructor.NewInstance(args));
        }
コード例 #3
0
        /// <summary>
        /// Create an instance of the given type using it's default constructor.
        /// </summary>
        public static object CreateInstance(Type type)
        {
            if (NullableReflection.TreatAsSystemNullableT(type))
            {
                return(null);
            }

            var genericInstance = GenericInstanceFactory.CreateGenericInstance(type);

            if (genericInstance != null)
            {
                return(genericInstance);
            }

            if (type.IsPrimitive)
            {
                return(TypeHelper.GetPrimitiveDefault(type));
            }

            return(type.NewInstance());
        }
コード例 #4
0
        /// <summary>
        /// Create an instance of the given type T using it's default constructor.
        /// </summary>
        public static T CreateInstance <T>()
        {
            var type = typeof(T);

            if (NullableReflection.TreatAsSystemNullableT(type))
            {
                return(default(T));
            }

            var genericInstance = GenericInstanceFactory.CreateGenericInstance(type);

            if (genericInstance != null)
            {
                return((T)genericInstance);
            }

            if (type.IsPrimitive)
            {
                return((T)TypeHelper.GetPrimitiveDefault(type));
            }

            return((T)type.NewInstance());
        }
コード例 #5
0
 public static Type GetUnderlyingType(Type nullableType)
 {
     return(NullableReflection.GetUnderlyingType(nullableType));
 }