예제 #1
0
        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));
 }