ApplicationManager.CreateObject メソッド

定義

指定したアプリケーション ドメインおよびオブジェクト型のオブジェクトを作成します。

オーバーロード

CreateObject(IApplicationHost, Type)

種類に基づいて、指定されたアプリケーション ドメインのオブジェクトを作成します。

CreateObject(String, Type, String, String, 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

パラメーター

appHost
IApplicationHost

IApplicationHost オブジェクト。

type
Type

作成するオブジェクトの型。

戻り値

type で指定された型の新しいオブジェクト。

例外

アプリケーションの物理パスが存在しません。

appHostnullです。

または

typenullです。

注釈

CreateObjectは、.NET Framework バージョン 3.5 で導入されています。 詳細については、「.NET Framework のバージョンおよび依存関係」を参照してください。

適用対象

CreateObject(String, Type, String, String, 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

戻り値

指定した type の新しいオブジェクト。

例外

physicalPathnull です

- または -

physicalPath が有効なアプリケーション パスではありません。

- または -

typeIRegisteredObject インターフェイスを実装していません。

appIDnullです。

または

typenullです。

failIfExiststrue で、指定した型のオブジェクトが既に登録されています。

次のコード例は、登録済みオブジェクトのオブジェクト ファクトリ デザイン パターンの実装です。 ファクトリ パターンを使用すると、オブジェクトの複数のインスタンスを登録できます。 この例には、 の 2 つのオブジェクト 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 、アプリケーションでオブジェクトを作成および登録するために使用されます。 各型のオブジェクトを 1 つだけ作成できます。 同じ型の複数のオブジェクトを作成する必要がある場合は、オブジェクト ファクトリを実装する必要があります。 詳細については、このトピックのコード例を参照してください。

一意のアプリケーション識別子によって識別される各アプリケーションは、独自のアプリケーション ドメインで実行されます。 メソッドは CreateObject 、 パラメーターで指定されたアプリケーションのアプリケーション ドメインに、指定した型のオブジェクトを appID 作成します。 指定したアプリケーションに対してアプリケーション ドメインが存在しない場合は、オブジェクトが作成される前に作成されます。

パラメーターは failIfExists 、指定した型のオブジェクトがアプリケーションに CreateObject 既に存在する場合のメソッドの動作を制御します。 が の場合failIfExists、メソッドはCreateObject例外をInvalidOperationExceptionスローtrueします。

falseの場合failIfExists、メソッドはCreateObject、指定した型の既存の登録済みオブジェクトを返します。

メソッドはCreateObject、 を に設定して追加throwOnErrorのパラメーターを受け取るオーバーロードをthrowOnErrorfalse呼び出します。

適用対象

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

戻り値

指定した type の新しいオブジェクト。

例外

physicalPathnull です

- または -

physicalPath が有効なアプリケーション パスではありません。

- または -

typeIRegisteredObject インターフェイスを実装していません。

appIDnullです。

または

typenullです。

failIfExiststrue で、指定した型のオブジェクトが既に登録されています。

注釈

メソッドのこのオーバーロードは CreateObject 、 パラメーターを throwOnError 提供します。これにより、ホスティング初期化例外がスローされるかどうかを制御できます。 を提供throwOnErrorしないメソッドのCreateObjectオーバーロードは、 パラメーターが に設定された状態でこのオーバーロードをfalse呼び出します。

適用対象