This documentation is archived and is not being maintained.

ApplicationManager::CreateObject Method (String, Type, String, String, Boolean)

Creates an object for the specified application domain based on type, virtual and physical paths, and a Boolean value indicating failure behavior when an object of the specified type already exists.

Namespace:  System.Web.Hosting
Assembly:  System.Web (in System.Web.dll)

[SecurityPermissionAttribute(SecurityAction::Demand, UnmanagedCode = true)]
public:
IRegisteredObject^ CreateObject(
	String^ appId, 
	Type^ type, 
	String^ virtualPath, 
	String^ physicalPath, 
	bool failIfExists
)

Parameters

appId
Type: System::String
The unique identifier for the application that owns the object.
type
Type: System::Type
The type of the object to create.
virtualPath
Type: System::String
The virtual path to the application.
physicalPath
Type: System::String
The physical path to the application.
failIfExists
Type: System::Boolean
true to throw an exception if an object of the specified type is currently registered; false to return the existing registered object of the specified type.

Return Value

Type: System.Web.Hosting::IRegisteredObject
A new object of the specified type.

ExceptionCondition
ArgumentException

physicalPath is nullptr

- or -

physicalPath is not a valid application path.

- or -

type does not implement the IRegisteredObject interface.

ArgumentNullException

appID is nullptr.

- or -

type is nullptr.

InvalidOperationException

failIfExists is true and an object of the specified type is already registered.

The CreateObject method is used to create and register objects in the application. Only one object of each type can be created. If you need to create multiple objects of the same type, you must implement an object factory. For more information, see the code example in this topic.

Each application, identified by a unique application identifier, runs in its own application domain. The CreateObject method creates an object of the specified type in the application domain of the application specified in the appID parameter. If an application domain does not exist for the specified application, one is created before the object is created.

The failIfExists parameter controls the behavior of the CreateObject method when an object of the specified type already exists in the application. When failIfExists is true, the CreateObject method throws an InvalidOperationException exception.

When failIfExists is false, the CreateObject method returns the existing registered object of the specified type.

The CreateObject method calls the overload that takes an additional throwOnError parameter with throwOnError set to false.

The following code example is an implementation of the object-factory design pattern for registered objects. Using the factory pattern enables you to register multiple instances of an object. The example contains two objects: SampleComponent, which is the object the application will use multiple instances of, and SampleComponentFactory, which manages a list of SampleComponent instances.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: