System.ComponentModel.TypeDescriptor.CreateInstance C# (CSharp) Method

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
        public static object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
        {
            if (objectType == null)
            {
                throw new ArgumentNullException(nameof(objectType));
            }

            if (argTypes != null)
            {
                if (args == null)
                {
                    throw new ArgumentNullException(nameof(args));
                }

                if (argTypes.Length != args.Length)
                {
                    throw new ArgumentException(SR.TypeDescriptorArgsCountMismatch);
                }
            }

            object instance = null;

            // See if the provider wants to offer a TypeDescriptionProvider to delegate to.  This allows
            // a caller to have complete control over all object instantiation.
            if (provider != null)
            {
                TypeDescriptionProvider p = provider.GetService(typeof(TypeDescriptionProvider)) as TypeDescriptionProvider;
                if (p != null)
                {
                    instance = p.CreateInstance(provider, objectType, argTypes, args);
                }
            }

            if (instance == null)
            {
                instance = NodeFor(objectType).CreateInstance(provider, objectType, argTypes, args);
            }

            return instance;
        }
        /// <summary>

Usage Example

        /// <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);
        }
All Usage Examples Of System.ComponentModel.TypeDescriptor::CreateInstance