Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

AppDomain.CurrentDomain Property

Gets the current application domain for the current Thread.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static AppDomain CurrentDomain { get; }

Property Value

Type: System.AppDomain
The current application domain.

The following example shows how to use the CurrentDomain property when you create a dynamic assembly in the current application domain.

using System;
using System.Reflection;
using System.Reflection.Emit;

class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      // In this version of the runtime, an assembly consists of one 
      // module which contains zero or more types. This example 
      // creates an assembly containing one public type named
      // "MyDynamicType". The type has a private field, a property 
      // that gets and sets the private field, constructors that 
      // initialize the private field, and a method that multiplies 
      // a user-supplied number by the private field value and returns
      // the result. In C# the type might look like this:
      public class MyDynamicType
          private int m_number;

          public MyDynamicType() : this(42) {}
          public MyDynamicType(int initNumber)
              m_number = initNumber;

          public int Number
              get { return m_number; }
              set { m_number = value; }

          public int MyMethod(int multiplier)
              return m_number * multiplier;

      AssemblyName aName = new AssemblyName("DynamicAssemblyExample");
      AssemblyBuilder ab =

      // Create the module.
      ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);

      TypeBuilder tb = mb.DefineType(

      // Add a private field of type int (Int32).
      FieldBuilder fbNumber = tb.DefineField(

      // Define a constructor that takes an integer argument and 
      // stores it in the private field. 
      Type[] parameterTypes = { typeof(int) };
      ConstructorBuilder ctor1 = tb.DefineConstructor(

      ILGenerator ctor1IL = ctor1.GetILGenerator();
      // For a constructor, argument zero is a reference to the new
      // instance. Push it on the stack before calling the base
      // class constructor. Specify the default constructor of the 
      // base class (System.Object) by passing an empty array of 
      // types (Type.EmptyTypes) to GetConstructor.
      // Push the instance on the stack before pushing the argument
      // that is to be assigned to the private field m_number.
      ctor1IL.Emit(OpCodes.Stfld, fbNumber);

      // Define a default constructor that supplies a default value
      // for the private field. For parameter types, pass the empty
      // array of types or pass null.
      ConstructorBuilder ctor0 = tb.DefineConstructor(

      ILGenerator ctor0IL = ctor0.GetILGenerator();
      // For a constructor, argument zero is a reference to the new
      // instance. Push it on the stack before pushing the default
      // value on the stack, then call constructor ctor1.
      ctor0IL.Emit(OpCodes.Ldc_I4_S, 42);
      ctor0IL.Emit(OpCodes.Call, ctor1);

      // Define a property named Number that gets and sets the private 
      // field.
      // The last argument of DefineProperty is null, because the
      // property has no parameters. (If you don't specify null, you must
      // specify an array of Type objects. For a parameterless property,
      // use the built-in array with no elements: Type.EmptyTypes)
      PropertyBuilder pbNumber = tb.DefineProperty(

      // The property "set" and property "get" methods require a special
      // set of attributes.
      MethodAttributes getSetAttr = MethodAttributes.Public |
          MethodAttributes.SpecialName | MethodAttributes.HideBySig;

      // Define the "get" accessor method for Number. The method returns
      // an integer and has no arguments. (Note that null could be 
      // used instead of Types.EmptyTypes)
      MethodBuilder mbNumberGetAccessor = tb.DefineMethod(

      ILGenerator numberGetIL = mbNumberGetAccessor.GetILGenerator();
      // For an instance property, argument zero is the instance. Load the 
      // instance, then load the private field and return, leaving the
      // field value on the stack.
      numberGetIL.Emit(OpCodes.Ldfld, fbNumber);

      // Define the "set" accessor method for Number, which has no return
      // type and takes one argument of type int (Int32).
      MethodBuilder mbNumberSetAccessor = tb.DefineMethod(
          new Type[] { typeof(int) });

      ILGenerator numberSetIL = mbNumberSetAccessor.GetILGenerator();
      // Load the instance and then the numeric argument, then store the
      // argument in the field.
      numberSetIL.Emit(OpCodes.Stfld, fbNumber);

      // Last, map the "get" and "set" accessor methods to the 
      // PropertyBuilder. The property is now complete. 

      // Define a method that accepts an integer argument and returns
      // the product of that integer and the private field m_number. This
      // time, the array of parameter types is created on the fly.
      MethodBuilder meth = tb.DefineMethod(
          new Type[] { typeof(int) });

      ILGenerator methIL = meth.GetILGenerator();
      // To retrieve the private instance field, load the instance it
      // belongs to (argument zero). After loading the field, load the 
      // argument one and then multiply. Return from the method with 
      // the return value (the product of the two numbers) on the 
      // execution stack.
      methIL.Emit(OpCodes.Ldfld, fbNumber);

      // Finish the type.
      Type t = tb.CreateType();

      // The code can be executed immediately. Start by getting reflection
      // objects for the method and the property.
      MethodInfo mi = t.GetMethod("MyMethod");
      PropertyInfo pi = t.GetProperty("Number");

      // Create an instance of MyDynamicType using the default 
      // constructor. 
      object o1 = Activator.CreateInstance(t);

      // Display the value of the property, then change it to 127 and 
      // display it again. Use null to indicate that the property
      // has no index.
      outputBlock.Text += String.Format("o1.Number: {0}\n", pi.GetValue(o1, null));
      pi.SetValue(o1, 127, null);
      outputBlock.Text += String.Format("o1.Number: {0}\n", pi.GetValue(o1, null));

      // Call MyMethod, passing 22, and display the return value, 22
      // times 127. Arguments must be passed as an array, even when
      // there is only one.
      object[] arguments = { 22 };
      outputBlock.Text += String.Format("o1.MyMethod(22): {0}\n",
          mi.Invoke(o1, arguments));

      // Create an instance of MyDynamicType using the constructor
      // that specifies m_Number. The constructor is identified by
      // matching the types in the argument array. In this case, 
      // the argument array is created on the fly. Display the 
      // property value.
      object o2 = Activator.CreateInstance(t,
          new object[] { 5280 });
      outputBlock.Text += String.Format("o2.Number: {0}\n", pi.GetValue(o2, null));

/* This code produces the following output:

o1.Number: 42
o1.Number: 127
o1.MyMethod(22): 2794
o2.Number: 5280


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.