CreateInstance() public static method

This method will search internal tables within TypeDescriptor for a TypeDescriptionProvider object that is associated with the given data type. If it finds one, it will delegate the call to that object.
public static CreateInstance ( IServiceProvider provider, Type objectType, Type argTypes, object args ) : object
provider IServiceProvider
objectType System.Type
argTypes System.Type
args object
return object
        /// <summary>Creates an instance of the specified type.</summary>
        /// <returns>A new instance of the type.</returns>
        /// <param name="type">A <see cref="T:System.Type" /> that represents the type to create. </param>
        protected object CreateInstance(Type type)
        {
            if (type == null || this.PropertyType == null)
            {
                return(null);
            }
            Type[] array = new Type[]
            {
                typeof(Type)
            };
            ConstructorInfo constructor = type.GetConstructor(array);
            object          result;

            if (constructor != null)
            {
                object[] args = new object[]
                {
                    this.PropertyType
                };
                result = TypeDescriptor.CreateInstance(null, type, array, args);
            }
            else
            {
                result = TypeDescriptor.CreateInstance(null, type, null, null);
            }
            return(result);
        }
Ejemplo n.º 2
0
 protected object CreateInstance(Type type)
 {
     Type[] types = new Type[] { typeof(Type) };
     if (type.GetConstructor(types) != null)
     {
         return(TypeDescriptor.CreateInstance(null, type, types, new object[] { this.PropertyType }));
     }
     return(TypeDescriptor.CreateInstance(null, type, null, null));
 }
        /// <summary>
        /// Creates an instance of the specified type.
        /// </summary>
        protected object CreateInstance(Type type)
        {
            Type[] typeArgs = new Type[] { typeof(Type) };
            ConstructorInfo ctor = type.GetConstructor(typeArgs);
            if (ctor != null)
            {
                return TypeDescriptor.CreateInstance(null, type, typeArgs, new object[] { PropertyType });
            }

            return TypeDescriptor.CreateInstance(null, type, null, null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates an instance of the specified type.
        /// </summary>
        protected object?CreateInstance(
            [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type type)
        {
            Type[]          typeArgs = new Type[] { typeof(Type) };
            ConstructorInfo?ctor     = type.GetConstructor(typeArgs);

            if (ctor != null)
            {
                return(TypeDescriptor.CreateInstance(null, type, typeArgs, new object[] { PropertyType }));
            }

            return(TypeDescriptor.CreateInstance(null, type, null, null));
        }
Ejemplo n.º 5
0
        protected object CreateInstance(Type type)
        {
            if (type == null || PropertyType == null)
            {
                return(null);
            }

            object instance = null;

            Type[]          paramTypes = new Type[] { typeof(Type) };
            ConstructorInfo ctor       = type.GetConstructor(paramTypes);

            if (ctor != null)
            {
                object[] parameters = new object[] { PropertyType };
                instance = TypeDescriptor.CreateInstance(null, type, paramTypes, parameters);
            }
            else
            {
                instance = TypeDescriptor.CreateInstance(null, type, null, null);
            }
            return(instance);
        }
Ejemplo n.º 6
0
        /// <summary>Creates an instance of the specified type.</summary>
        /// <returns>A new instance of the type.</returns>
        /// <param name="type">A <see cref="T:System.Type" /> that represents the type to create. </param>
        protected object CreateInstance(Type type)
        {
            if (type == null || PropertyType == null)
            {
                return(null);
            }
            object obj = null;

            Type[] array = new Type[1]
            {
                typeof(Type)
            };
            ConstructorInfo constructor = type.GetConstructor(array);

            if (constructor != null)
            {
                object[] args = new object[1]
                {
                    PropertyType
                };
                return(TypeDescriptor.CreateInstance(null, type, array, args));
            }
            return(TypeDescriptor.CreateInstance(null, type, null, null));
        }