ApplicationManager.CreateObject 方法
定义
为指定的应用程序域和对象类型创建一个对象。Creates an object for the specified application domain and object type.
重载
| CreateObject(IApplicationHost, Type) |
基于类型创建指定应用程序域的对象。Creates an object for the specified application domain, based on type. |
| CreateObject(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. |
| CreateObject(String, Type, String, String, Boolean, Boolean) |
基于以下参数为指定的应用程序域创建一个对象:类型、虚拟路径和物理路径、一个指示当指定类型的对象已存在时的失败行为的布尔值,以及一个指示是否引发宿主初始化错误异常的布尔值。Creates an object for the specified application domain based on type, virtual and physical paths, a Boolean value indicating failure behavior when an object of the specified type already exists, and a Boolean value indicating whether hosting initialization error exceptions are thrown. |
CreateObject(IApplicationHost, Type)
基于类型创建指定应用程序域的对象。Creates an object for the specified application domain, based on type.
public:
System::Web::Hosting::IRegisteredObject ^ CreateObject(System::Web::Hosting::IApplicationHost ^ appHost, Type ^ type);
public System.Web.Hosting.IRegisteredObject CreateObject (System.Web.Hosting.IApplicationHost appHost, Type type);
member this.CreateObject : System.Web.Hosting.IApplicationHost * Type -> System.Web.Hosting.IRegisteredObject
Public Function CreateObject (appHost As IApplicationHost, type As Type) As IRegisteredObject
参数
- appHost
- IApplicationHost
一个 IApplicationHost 对象。An IApplicationHost object.
- type
- Type
要创建的对象类型。The type of the object to create.
返回
type 中指定的类型的新对象。A new object of the type specified in type.
例外
应用程序的物理路径不存在。A physical path for the application does not exist.
注解
CreateObject .NET Framework 版本3.5 中引入。CreateObject is introduced in the .NET Framework version 3.5. 有关详细信息,请参见版本和依赖关系。For more information, see Versions and Dependencies.
适用于
CreateObject(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.
public:
System::Web::Hosting::IRegisteredObject ^ CreateObject(System::String ^ appId, Type ^ type, System::String ^ virtualPath, System::String ^ physicalPath, bool failIfExists);
public System.Web.Hosting.IRegisteredObject CreateObject (string appId, Type type, string virtualPath, string physicalPath, bool failIfExists);
member this.CreateObject : string * Type * string * string * bool -> System.Web.Hosting.IRegisteredObject
Public Function CreateObject (appId As String, type As Type, virtualPath As String, physicalPath As String, failIfExists As Boolean) As IRegisteredObject
参数
- appId
- String
拥有该对象的应用程序的唯一标识符。The unique identifier for the application that owns the object.
- type
- Type
要创建的对象类型。The type of the object to create.
- virtualPath
- String
应用程序的虚拟路径。The virtual path to the application.
- physicalPath
- String
应用程序的物理路径。The physical path to the application.
- failIfExists
- Boolean
若为 true 则在指定类型的对象当前已注册的情况下引发一个异常;若为 false 则返回现有已注册的指定类型的对象。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.
返回
指定 type 的新对象。A new object of the specified type.
例外
physicalPath 为 nullphysicalPath is null
- 或 --or-
physicalPath 不是有效的应用程序路径。physicalPath is not a valid application path.
- 或 --or-
type 不实现 IRegisteredObject 接口。type does not implement the IRegisteredObject interface.
failIfExists 为 true,且已经注册指定类型的对象。failIfExists is true and an object of the specified type is already registered.
示例
下面的代码示例是已注册对象的对象工厂设计模式的实现。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. 该示例包含两个对象: SampleComponent ,这是一个对象,应用程序将使用多个实例(和 SampleComponentFactory )来管理实例的列表 SampleComponent 。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.
using System.Web.Hosting;
public class SampleComponentFactory : IRegisteredObject
{
private ArrayList components = new ArrayList();
public void Start()
{
HostingEnvironment.RegisterObject(this);
}
void IRegisteredObject.Stop(bool immediate)
{
foreach (SampleComponent c in components)
{
((IRegisteredObject)c).Stop(immediate);
}
HostingEnvironment.UnregisterObject(this);
}
public SampleComponent CreateComponent()
{
SampleComponent newComponent = new SampleComponent();
newComponent.Initialize();
components.Add(newComponent);
return newComponent;
}
}
public class SampleComponent : IRegisteredObject
{
void IRegisteredObject.Stop(bool immediate)
{
// Clean up component resources here.
}
public void Initialize()
{
// Initialize component here.
}
}
Imports System.Web.Hosting
Public Class SampleComponentFactory
Implements IRegisteredObject
Dim components As ArrayList = New ArrayList()
Public Sub Start()
HostingEnvironment.RegisterObject(Me)
End Sub
Public Sub [Stop](ByVal immediate As Boolean) Implements System.Web.Hosting.IRegisteredObject.Stop
For Each c As SampleComponent In components
CType(c, IRegisteredObject).Stop(immediate)
Next
HostingEnvironment.UnregisterObject(Me)
End Sub
Public Function CreateComponent() As SampleComponent
Dim newComponent As SampleComponent
newComponent = New SampleComponent
newComponent.Initialize()
components.Add(newComponent)
Return newComponent
End Function
End Class
Public Class SampleComponent
Implements IRegisteredObject
Sub [Stop](ByVal immediate As Boolean) Implements System.Web.Hosting.IRegisteredObject.Stop
' Clean up component resources here.
End Sub
Public Sub Initialize()
' Initialize component here.
End Sub
End Class
注解
CreateObject方法用于在应用程序中创建和注册对象。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. CreateObject方法在参数中指定的应用程序的应用程序域中创建指定类型的对象 appID 。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.
failIfExists CreateObject 当应用程序中已存在指定类型的对象时,参数控制该方法的行为。The failIfExists parameter controls the behavior of the CreateObject method when an object of the specified type already exists in the application. 当 failIfExists 为时 true ,该 CreateObject 方法将引发 InvalidOperationException 异常。When failIfExists is true, the CreateObject method throws an InvalidOperationException exception.
如果 failIfExists 为 false ,则此 CreateObject 方法返回指定类型的现有已注册对象。When failIfExists is false, the CreateObject method returns the existing registered object of the specified type.
CreateObject方法调用重载,该重载采用 throwOnError 设置为的附加参数 throwOnError false 。The CreateObject method calls the overload that takes an additional throwOnError parameter with throwOnError set to false.
适用于
CreateObject(String, Type, String, String, Boolean, Boolean)
基于以下参数为指定的应用程序域创建一个对象:类型、虚拟路径和物理路径、一个指示当指定类型的对象已存在时的失败行为的布尔值,以及一个指示是否引发宿主初始化错误异常的布尔值。Creates an object for the specified application domain based on type, virtual and physical paths, a Boolean value indicating failure behavior when an object of the specified type already exists, and a Boolean value indicating whether hosting initialization error exceptions are thrown.
public:
System::Web::Hosting::IRegisteredObject ^ CreateObject(System::String ^ appId, Type ^ type, System::String ^ virtualPath, System::String ^ physicalPath, bool failIfExists, bool throwOnError);
public System.Web.Hosting.IRegisteredObject CreateObject (string appId, Type type, string virtualPath, string physicalPath, bool failIfExists, bool throwOnError);
member this.CreateObject : string * Type * string * string * bool * bool -> System.Web.Hosting.IRegisteredObject
Public Function CreateObject (appId As String, type As Type, virtualPath As String, physicalPath As String, failIfExists As Boolean, throwOnError As Boolean) As IRegisteredObject
参数
- appId
- String
拥有该对象的应用程序的唯一标识符。The unique identifier for the application that owns the object.
- type
- Type
要创建的对象类型。The type of the object to create.
- virtualPath
- String
应用程序的虚拟路径。The virtual path to the application.
- physicalPath
- String
应用程序的物理路径。The physical path to the application.
- failIfExists
- Boolean
若为 true 则在指定类型的对象当前已注册的情况下引发一个异常;若为 false 则返回现有已注册的指定类型的对象。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.
- throwOnError
- Boolean
若为 true 则引发宿主初始化错误异常;若为 false 则不引发宿主初始化异常。true to throw exceptions for hosting initialization errors; false to not throw hosting initialization exceptions.
返回
指定 type 的新对象。A new object of the specified type.
例外
physicalPath 为 nullphysicalPath is null
- 或 --or-
physicalPath 不是有效的应用程序路径。physicalPath is not a valid application path.
- 或 --or-
type 不实现 IRegisteredObject 接口。type does not implement the IRegisteredObject interface.
failIfExists 为 true,且已经注册指定类型的对象。failIfExists is true and an object of the specified type is already registered.
注解
此方法的重载 CreateObject 提供 throwOnError 参数,该参数允许您控制是否引发宿主初始化异常。This overload of the CreateObject method provides the throwOnError parameter, which allows you to control whether hosting initialization exceptions are thrown. CreateObject不提供调用此重载的方法的重载, throwOnError 参数设置为 false 。The overload of the CreateObject method that does not provide throwOnError calls this overload with the parameter set to false.