ApplicationManager.CreateObject Metodo

Definizione

Crea un oggetto per il dominio applicazione e il tipo di oggetto specificati.

Overload

Nome Descrizione
CreateObject(IApplicationHost, Type)

Crea un oggetto per il dominio applicazione specificato, in base al tipo.

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

Crea un oggetto per il dominio applicazione specificato in base al tipo, ai percorsi fisici e virtuali e un valore booleano che indica il comportamento di errore quando esiste già un oggetto del tipo specificato.

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

Crea un oggetto per il dominio applicazione specificato in base al tipo, ai percorsi virtuali e fisici, a un valore booleano che indica il comportamento di errore quando esiste già un oggetto del tipo specificato e un valore booleano che indica se vengono generate eccezioni di errore di inizializzazione dell'hosting.

CreateObject(IApplicationHost, Type)

Crea un oggetto per il dominio applicazione specificato, in base al tipo.

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

Parametri

type
Type

Tipo dell'oggetto da creare.

Valori restituiti

Nuovo oggetto del tipo specificato in type.

Eccezioni

Non esiste un percorso fisico per l'applicazione.

appHost è null.

oppure

type è null.

Commenti

CreateObject è stato introdotto in .NET Framework versione 3.5. Per altre informazioni, vedere Versioni e dipendenze.

Si applica a

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

Crea un oggetto per il dominio applicazione specificato in base al tipo, ai percorsi fisici e virtuali e un valore booleano che indica il comportamento di errore quando esiste già un oggetto del tipo specificato.

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

Parametri

appId
String

Identificatore univoco per l'applicazione proprietaria dell'oggetto .

type
Type

Tipo dell'oggetto da creare.

virtualPath
String

Percorso virtuale dell'applicazione.

physicalPath
String

Percorso fisico dell'applicazione.

failIfExists
Boolean

true per generare un'eccezione se un oggetto del tipo specificato è attualmente registrato; false per restituire l'oggetto registrato esistente del tipo specificato.

Valori restituiti

Nuovo oggetto dell'oggetto specificato type.

Eccezioni

physicalPath è null

oppure

physicalPath non è un percorso applicazione valido.

oppure

type non implementa l'interfaccia IRegisteredObject .

appID è null.

oppure

type è null.

failIfExists è true e un oggetto del tipo specificato è già registrato.

Esempio

L'esempio di codice seguente è un'implementazione del modello di progettazione object factory per gli oggetti registrati. L'uso del modello factory consente di registrare più istanze di un oggetto. L'esempio contiene due oggetti: SampleComponent, ovvero l'oggetto di cui l'applicazione userà più istanze e SampleComponentFactory, che gestisce un elenco di SampleComponent istanze.

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

Commenti

Il CreateObject metodo viene usato per creare e registrare oggetti nell'applicazione. È possibile creare un solo oggetto di ogni tipo. Se è necessario creare più oggetti dello stesso tipo, è necessario implementare una factory di oggetti. Per altre informazioni, vedere l'esempio di codice in questo argomento.

Ogni applicazione, identificata da un identificatore univoco dell'applicazione, viene eseguita nel proprio dominio applicazione. Il CreateObject metodo crea un oggetto del tipo specificato nel dominio applicazione dell'applicazione specificato nel appID parametro . Se non esiste un dominio applicazione per l'applicazione specificata, ne viene creato uno prima della creazione dell'oggetto.

Il failIfExists parametro controlla il comportamento del CreateObject metodo quando un oggetto del tipo specificato esiste già nell'applicazione. Quando failIfExists è true, il CreateObject metodo genera un'eccezione InvalidOperationException .

Quando failIfExists è false, il CreateObject metodo restituisce l'oggetto registrato esistente del tipo specificato.

Il CreateObject metodo chiama l'overload che accetta un parametro aggiuntivo throwOnError con throwOnError impostato su false.

Si applica a

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

Crea un oggetto per il dominio applicazione specificato in base al tipo, ai percorsi virtuali e fisici, a un valore booleano che indica il comportamento di errore quando esiste già un oggetto del tipo specificato e un valore booleano che indica se vengono generate eccezioni di errore di inizializzazione dell'hosting.

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

Parametri

appId
String

Identificatore univoco per l'applicazione proprietaria dell'oggetto .

type
Type

Tipo dell'oggetto da creare.

virtualPath
String

Percorso virtuale dell'applicazione.

physicalPath
String

Percorso fisico dell'applicazione.

failIfExists
Boolean

true per generare un'eccezione se un oggetto del tipo specificato è attualmente registrato; false per restituire l'oggetto registrato esistente del tipo specificato.

throwOnError
Boolean

true per generare eccezioni per l'hosting degli errori di inizializzazione; false per non generare eccezioni di inizializzazione dell'hosting.

Valori restituiti

Nuovo oggetto dell'oggetto specificato type.

Eccezioni

physicalPath è null

oppure

physicalPath non è un percorso applicazione valido.

oppure

type non implementa l'interfaccia IRegisteredObject .

appID è null.

oppure

type è null.

failIfExists è true e un oggetto del tipo specificato è già registrato.

Commenti

Questo overload del CreateObject metodo fornisce il throwOnError parametro , che consente di controllare se vengono generate eccezioni di inizializzazione dell'hosting. Overload del CreateObject metodo che non fornisce throwOnError chiamate a questo overload con il parametro impostato su false.

Si applica a