Export (0) Print
Expand All

AppDomainSetup.AppDomainInitializer Property

Gets or sets the AppDomainInitializer delegate, which represents a callback method that is invoked when the application domain is initialized.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Property AppDomainInitializer As AppDomainInitializer

Property Value

Type: System.AppDomainInitializer
A delegate that represents a callback method that is invoked when the application domain is initialized.

To pass information to the callback method, assign an array of strings to the AppDomainInitializerArguments property. The array is passed to the callback method each time an AppDomain is initialized.

The callback method is executed in the context of the newly created application domain.

The following example creates a child application domain named ChildDomain, using an AppDomainSetup object and evidence from the default application domain. The AppDomainInitializer property is set to the callback method AppDomainInit, which is invoked when the child domain is initialized. The arguments for the callback method are placed in an array of strings, which is assigned to the AppDomainInitializerArguments property. When the child domain is created, the callback method simply prints the strings.

Imports System
Imports System.Security.Policy

Public Class Example

    Public Shared Sub Main()

        ' Get a reference to the default application domain. 
        
        Dim current As AppDomain = AppDomain.CurrentDomain

        ' Create the AppDomainSetup that will be used to set up the child 
        ' AppDomain. 
        Dim ads As New AppDomainSetup()

        ' Use the evidence from the default application domain to 
        ' create evidence for the child application domain. 
        
        Dim ev As Evidence = New Evidence(current.Evidence)

        ' Create an AppDomainInitializer delegate that represents the  
        ' callback method, AppDomainInit. Assign this delegate to the 
        ' AppDomainInitializer property of the AppDomainSetup object. 
        
        Dim adi As New AppDomainInitializer(AddressOf AppDomainInit)
        ads.AppDomainInitializer = adi

        ' Create an array of strings to pass as arguments to the callback 
        ' method. Assign the array to the AppDomainInitializerArguments 
        ' property. 
        Dim initArgs() As String = {"String1", "String2"}
        ads.AppDomainInitializerArguments = initArgs

        ' Create a child application domain named "ChildDomain", using  
        ' the evidence and the AppDomainSetup object. 
        
        Dim ad As AppDomain = _
            AppDomain.CreateDomain("ChildDomain", ev, ads)

        Console.WriteLine("Press the Enter key to exit the example program.")
        Console.ReadLine()
    End Sub 

    ' The callback method invoked when the child application domain is 
    ' initialized. The method simply displays the arguments that were 
    ' passed to it. 
    
    Public Shared Sub AppDomainInit(ByVal args() As String)
        Console.WriteLine("AppDomain ""{0}"" is initialized with these arguments:", _
            AppDomain.CurrentDomain.FriendlyName)
        For Each arg As String In args
            Console.WriteLine("    {0}", arg)
        Next 
    End Sub 
End Class 

' This code example produces the following output: 

'AppDomain "ChildDomain" is initialized with these arguments: 
'    String1 
'    String2

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft