Example #1
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));
        }
Example #2
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());
        }
Example #3
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());
        }