ApplicationManager.CreateObject Méthode

Définition

Crée un objet pour le domaine d’application et le type d’objet spécifiés.

Surcharges

Nom Description
CreateObject(IApplicationHost, Type)

Crée un objet pour le domaine d’application spécifié, en fonction du type.

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

Crée un objet pour le domaine d’application spécifié en fonction du type, des chemins d’accès virtuels et physiques, ainsi qu’une valeur booléenne indiquant le comportement d’échec lorsqu’un objet du type spécifié existe déjà.

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

Crée un objet pour le domaine d’application spécifié en fonction du type, des chemins virtuels et physiques, une valeur booléenne indiquant le comportement d’échec lorsqu’un objet du type spécifié existe déjà et une valeur booléenne indiquant si les exceptions d’erreur d’initialisation d’hébergement sont levées.

CreateObject(IApplicationHost, Type)

Crée un objet pour le domaine d’application spécifié, en fonction du 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

Paramètres

type
Type

Type de l’objet à créer.

Retours

Nouvel objet du type spécifié dans type.

Exceptions

Un chemin d’accès physique pour l’application n’existe pas.

appHost a la valeur null.

-ou-

type a la valeur null.

Remarques

CreateObject est introduit dans .NET Framework version 3.5. Pour plus d’informations, consultez Versions et dépendances.

S’applique à

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

Crée un objet pour le domaine d’application spécifié en fonction du type, des chemins d’accès virtuels et physiques, ainsi qu’une valeur booléenne indiquant le comportement d’échec lorsqu’un objet du type spécifié existe déjà.

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

Paramètres

appId
String

Identificateur unique de l’application propriétaire de l’objet.

type
Type

Type de l’objet à créer.

virtualPath
String

Chemin d’accès virtuel à l’application.

physicalPath
String

Chemin d’accès physique à l’application.

failIfExists
Boolean

true pour lever une exception si un objet du type spécifié est actuellement inscrit ; false pour renvoyer l’objet inscrit existant du type spécifié.

Retours

Nouvel objet de l’objet spécifié type.

Exceptions

physicalPath est null

-ou-

physicalPath n’est pas un chemin d’accès d’application valide.

-ou-

type n’implémente pas l’interface IRegisteredObject .

appID a la valeur null.

-ou-

type a la valeur null.

failIfExists est true et un objet du type spécifié est déjà inscrit.

Exemples

L’exemple de code suivant est une implémentation du modèle de conception d’usine d’objets pour les objets inscrits. L’utilisation du modèle de fabrique vous permet d’inscrire plusieurs instances d’un objet. L’exemple contient deux objets : SampleComponent, qui est l’objet dont l’application utilisera plusieurs instances et SampleComponentFactory, qui gère une liste d’instances 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

Remarques

La CreateObject méthode est utilisée pour créer et inscrire des objets dans l’application. Un seul objet de chaque type peut être créé. Si vous devez créer plusieurs objets du même type, vous devez implémenter une fabrique d’objets. Pour plus d’informations, consultez l’exemple de code de cette rubrique.

Chaque application, identifiée par un identificateur d’application unique, s’exécute dans son propre domaine d’application. La CreateObject méthode crée un objet du type spécifié dans le domaine d’application de l’application spécifiée dans le appID paramètre. Si un domaine d’application n’existe pas pour l’application spécifiée, un domaine est créé avant la création de l’objet.

Le failIfExists paramètre contrôle le comportement de la CreateObject méthode lorsqu’un objet du type spécifié existe déjà dans l’application. Quand failIfExists c’est truele cas, la CreateObject méthode lève une InvalidOperationException exception.

Quand failIfExists c’est falsele cas, la CreateObject méthode retourne l’objet inscrit existant du type spécifié.

La CreateObject méthode appelle la surcharge qui prend un paramètre supplémentaire throwOnError avec throwOnError défini sur false.

S’applique à

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

Crée un objet pour le domaine d’application spécifié en fonction du type, des chemins virtuels et physiques, une valeur booléenne indiquant le comportement d’échec lorsqu’un objet du type spécifié existe déjà et une valeur booléenne indiquant si les exceptions d’erreur d’initialisation d’hébergement sont levées.

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

Paramètres

appId
String

Identificateur unique de l’application propriétaire de l’objet.

type
Type

Type de l’objet à créer.

virtualPath
String

Chemin d’accès virtuel à l’application.

physicalPath
String

Chemin d’accès physique à l’application.

failIfExists
Boolean

true pour lever une exception si un objet du type spécifié est actuellement inscrit ; false pour renvoyer l’objet inscrit existant du type spécifié.

throwOnError
Boolean

true pour lever des exceptions pour les erreurs d’initialisation d’hébergement ; false pour ne pas lever d’exceptions d’initialisation d’hébergement.

Retours

Nouvel objet de l’objet spécifié type.

Exceptions

physicalPath est null

-ou-

physicalPath n’est pas un chemin d’accès d’application valide.

-ou-

type n’implémente pas l’interface IRegisteredObject .

appID a la valeur null.

-ou-

type a la valeur null.

failIfExists est true et un objet du type spécifié est déjà inscrit.

Remarques

Cette surcharge de la CreateObject méthode fournit le throwOnError paramètre, ce qui vous permet de contrôler si les exceptions d’initialisation d’hébergement sont levées. Surcharge de la CreateObject méthode qui ne fournit throwOnError pas d’appels avec le paramètre défini sur false.

S’applique à