AppDomainSetup.AppDomainInitializer Property

Note: This property is new in the .NET Framework version 2.0.

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)

public AppDomainInitializer AppDomainInitializer { get; set; }
/** @property */
public AppDomainInitializer get_AppDomainInitializer ()

/** @property */
public void set_AppDomainInitializer (AppDomainInitializer value)

public function get AppDomainInitializer () : AppDomainInitializer

public function set AppDomainInitializer (value : AppDomainInitializer)

Property Value

An AppDomainInitializer 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 code 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.

using System;
using System.Security.Policy;

public class Example
    public static void Main()
        // Get a reference to the default application domain.
        AppDomain current = AppDomain.CurrentDomain;

        // Create the AppDomainSetup that will be used to set up the child
        // AppDomain.
        AppDomainSetup ads = new AppDomainSetup();

        // Use the evidence from the default application domain to
        // create evidence for the child application domain.
        Evidence ev = 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.
        AppDomainInitializer adi = new AppDomainInitializer(AppDomainInit);
        ads.AppDomainInitializer = adi;

        // Create an array of strings to pass as arguments to the callback
        // method. Assign the array to the AppDomainInitializerArguments
        // property.
        string[] initArgs = {"String1", "String2"};
        ads.AppDomainInitializerArguments = initArgs;

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

        Console.WriteLine("Press the Enter key to exit the example program.");

    // The callback method invoked when the child application domain is
    // initialized. The method simply displays the arguments that were
    // passed to it.
    public static void AppDomainInit(string[] args)
        Console.WriteLine("AppDomain \"{0}\" is initialized with these arguments:", 
        foreach (string arg in args)
            Console.WriteLine("    {0}", arg);

/* This code example produces the following output:

AppDomain "ChildDomain" is initialized with these arguments:

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0