Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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)

public:
[SecurityPermissionAttribute(SecurityAction::Demand, UnmanagedCode = true)]
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.

Exception Condition
ArgumentException

physicalPath is null

- or -

physicalPath is not a valid application path.

- or -

type does not implement the IRegisteredObject interface.

ArgumentNullException

appID is null.

- or -

type is null.

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
Available since 2.0
Return to top
Show: