CreateInstance() public method

public CreateInstance ( IServiceProvider provider, Type objectType, Type argTypes, object args ) : object
provider IServiceProvider
objectType System.Type
argTypes System.Type
args object
return object
Esempio n. 1
0
        public static object CreateInstance(IServiceProvider provider, Type objectType, Type [] argTypes, object [] args)
        {
            if (objectType == null)
            {
                throw new ArgumentNullException("objectType");
            }

            object instance = null;

            if (provider != null)
            {
                TypeDescriptionProvider typeDescrProvider = provider.GetService(typeof(TypeDescriptionProvider)) as TypeDescriptionProvider;
                if (typeDescrProvider != null)
                {
                    instance = typeDescrProvider.CreateInstance(provider, objectType, argTypes, args);
                }
            }

            // TODO: also search and use the internal providers table once Add/RemoveProvider have been implemented

            if (instance == null)
            {
                instance = Activator.CreateInstance(objectType, args);
            }

            return(instance);
        }
 /// <summary>Creates an object that can substitute for another data type.</summary>
 /// <returns>The substitute <see cref="T:System.Object" />.</returns>
 /// <param name="provider">An optional service provider.</param>
 /// <param name="objectType">The type of object to create. This parameter is never null.</param>
 /// <param name="argTypes">An optional array of types that represent the parameter types to be passed to the object's constructor. This array can be null or of zero length.</param>
 /// <param name="args">An optional array of parameter values to pass to the object's constructor.</param>
 public virtual object CreateInstance(IServiceProvider provider, Type objectType, Type[] argTypes, object[] args)
 {
     if (_parent != null)
     {
         return(_parent.CreateInstance(provider, objectType, argTypes, args));
     }
     return(Activator.CreateInstance(objectType, args));
 }