ApplicationManager.CreateObject 方法

定義

建立指定之應用程式定義域的物件和物件型別。

多載

CreateObject(IApplicationHost, Type)

依據類型,針對指定的應用程式定義域建立物件。

CreateObject(String, Type, String, String, Boolean)

根據型別、虛擬路徑和實體路徑建立指定應用程式定義域的物件,如果指定型別的物件已存在,則會使用布林值 (Boolean) 表示失敗行為。

CreateObject(String, Type, String, String, Boolean, Boolean)

根據型別、虛擬路徑和實體路徑建立指定之應用程式定義域的物件,如果指定型別的物件已存在,則會使用布林值表示失敗行為,且布林值會指出是否擲回裝載初始設定錯誤例外狀況。

CreateObject(IApplicationHost, 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

參數

type
Type

要建立的物件類型。

傳回

IRegisteredObject

type 中指定之類型的新物件。

例外狀況

應用程式的實體路徑不存在。

appHostnull

-或- typenull

備註

CreateObject.NET Framework 3.5 版引進。 如需詳細資訊,請參閱版本和相依性

適用於

CreateObject(String, Type, String, String, Boolean)

根據型別、虛擬路徑和實體路徑建立指定應用程式定義域的物件,如果指定型別的物件已存在,則會使用布林值 (Boolean) 表示失敗行為。

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

應用程式的唯一識別項,該應用程式擁有物件。

type
Type

要建立的物件類型。

virtualPath
String

應用程式的虛擬路徑。

physicalPath
String

應用程式的實體路徑。

failIfExists
Boolean

true 表示如果指定型別的物件目前已註冊則擲回例外狀況,false 表示傳回指定型別的現有註冊物件。

傳回

IRegisteredObject

指定之 type 的新物件。

例外狀況

physicalPathnull

-或- physicalPath 不是有效的應用程式路徑。

-或- type 未實作 IRegisteredObject 介面。

appIDnull

-或- typenull

failIfExiststrue,且指定型別的物件已註冊。

範例

下列程式碼範例是已註冊物件的物件處理站設計模式實作。 使用 Factory 模式可讓您註冊物件的多個實例。 此範例包含兩個物件: SampleComponent ,這是應用程式將使用多個實例的物件,以及 SampleComponentFactory 管理實例清單的物件 SampleComponent

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 可用來在應用程式中建立和註冊物件。 每個類型只能建立一個物件。 如果您需要建立相同類型的多個物件,則必須實作物件處理站。 如需詳細資訊,請參閱本主題中的程式碼範例。

每個應用程式都是由唯一的應用程式識別碼所識別,會在自己的應用程式域中執行。 CreateObject方法會在 參數所 appID 指定之應用程式的應用程式域中,建立指定型別的物件。 如果指定的應用程式不存在應用程式域,則會在建立物件之前先建立一個。

參數 failIfExists 會控制當應用程式中已有指定型別的物件時,方法的行為 CreateObject 。 當 為 truefailIfExists ,方法 CreateObjectInvalidOperationException 擲回例外狀況。

當 為 falsefailIfExists ,此方法 CreateObject 會傳回指定型別的現有已註冊物件。

方法 CreateObject 會呼叫多載,這個多載會採用設定為 false 的其他 throwOnError 參數 throwOnError

適用於

CreateObject(String, Type, String, String, Boolean, Boolean)

根據型別、虛擬路徑和實體路徑建立指定之應用程式定義域的物件,如果指定型別的物件已存在,則會使用布林值表示失敗行為,且布林值會指出是否擲回裝載初始設定錯誤例外狀況。

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

應用程式的唯一識別項,該應用程式擁有物件。

type
Type

要建立的物件類型。

virtualPath
String

應用程式的虛擬路徑。

physicalPath
String

應用程式的實體路徑。

failIfExists
Boolean

true 表示如果指定型別的物件目前已註冊則擲回例外狀況,false 表示傳回指定型別的現有註冊物件。

throwOnError
Boolean

true,表示擲回裝載初始設定錯誤的例外狀況,false 表示不擲回裝載初始設定例外狀況。

傳回

IRegisteredObject

指定之 type 的新物件。

例外狀況

physicalPathnull

-或- physicalPath 不是有效的應用程式路徑。

-或- type 未實作 IRegisteredObject 介面。

appIDnull

-或- typenull

failIfExiststrue,且指定型別的物件已註冊。

備註

此方法的 CreateObject 這個多載提供 throwOnError 參數,可讓您控制是否擲回裝載初始化例外狀況。 未提供 throwOnError 這個多載的方法多載 CreateObject ,並將 參數設定為 false

適用於