Export (0) Print
Expand All

DynamicMethod.CreateDelegate Method (Type, Object)

Completes the dynamic method and creates a delegate that can be used to execute it, specifying the delegate type and an object the delegate is bound to.

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

[ComVisibleAttribute(true)]
[SecuritySafeCriticalAttribute]
public override sealed Delegate CreateDelegate(
	Type delegateType,
	Object target
)

Parameters

delegateType
Type: System.Type
A delegate type whose signature matches that of the dynamic method, minus the first parameter.
target
Type: System.Object
An object the delegate is bound to. Must be of the same type as the first parameter of the dynamic method.

Return Value

Type: System.Delegate
A delegate of the specified type, which can be used to execute the dynamic method with the specified target object.

ExceptionCondition
InvalidOperationException

The dynamic method has no method body.

ArgumentException

target is not the same type as the first parameter of the dynamic method, and is not assignable to that type.

-or-

delegateType has the wrong number of parameters or the wrong parameter types.

This method overload creates a delegate bound to a particular object. Such a delegate is said to be closed over its first argument. Although the method is static, it acts as if it were an instance method; the instance is target.

This method overload requires target to be of the same type as the first parameter of the dynamic method, or to be assignable to that type (for example, a derived class). The signature of delegateType has all the parameters of the dynamic method except the first. For example, if the dynamic method has the parameters String, Int32, and Byte, then delegateType has the parameters Int32 and Byte; target is of type String.

Calling the CreateDelegate method or the Invoke method completes the dynamic method. Any further attempt to alter the dynamic method, such as modifying parameter definitions or emitting more Microsoft intermediate language (MSIL), is ignored; no exception is thrown.

The following example shows how to define a dynamic method, and then create a delegate with an object that is bound to the first parameter of the method.

The example defines a class named Base with a field named Number, and a class named DerivedFromBase that derives from the first class. The example defines two delegate types: one named UseLikeStatic that returns Int32 and has parameters of type Base and Int32, and another named UseLikeInstance that returns Int32 and has one parameter of type Int32.

The example then creates a DynamicMethod that changes the Number field of an instance of Base and returns the previous value.

The example creates an instance of Base and then creates two delegates. The first delegate is of type UseLikeStatic, which has the same parameters as the dynamic method. The second delegate is of type UseLikeInstance, which lacks the first parameter (of type Base). This delegate is created by using the CreateDelegate(Type, Object) method overload; the second parameter of that method overload is an instance of Base, which is bound to the newly created delegate. Whenever that delegate is invoked, the dynamic method acts on the bound instance of Base.

NoteNote:

This is an example of the relaxed rules for delegate binding, which are also supported by the Delegate.CreateDelegate method. For more information, see the Delegate class.

The UseLikeStatic delegate is invoked, passing in the same instance of Base that is bound to the UseLikeInstance delegate. Then the UseLikeInstance delegate is invoked, so that both delegates act on the same instance of Base. The changes in the values of the Number field are displayed after each call. Finally, a UseLikeInstance delegate is bound to an instance of DerivedFromBase, and the delegate calls are repeated.


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

// These classes are for demonstration purposes.
//
public class Base
{
   public int Number = 0;
   public Base(int initialNumber)
   {
      this.Number = initialNumber;
   }
}

public class DerivedFromBase : Base
{
   public DerivedFromBase(int initialNumber) : base(initialNumber) { }
}

// Two delegates are declared: UseLikeInstance treats the dynamic
// method as if it were an instance method, and UseLikeStatic
// treats the dynamic method in the ordinary fashion.
// 
public delegate int UseLikeInstance(int newNumber);
public delegate int UseLikeStatic(Base ba, int newNumber);

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // This dynamic method sets the public Number field of an instance
      // of Base, and returns the previous value. The method has no name. 
      // It takes two parameters, an instance of Base and an Integer that
      // is the new field value. 
      //
      DynamicMethod changeNumber = new DynamicMethod(
          "",
          typeof(int),
          new Type[] { typeof(Base), typeof(int) }
      );

      // Get a FieldInfo for the public field 'Number'.
      FieldInfo fnum = typeof(Base).GetField(
          "Number",
          BindingFlags.Public | BindingFlags.Instance
      );

      ILGenerator ilg = changeNumber.GetILGenerator();

      // Push the current value of the Number field onto the 
      // evaluation stack. It's an instance field, so load the
      // instance of Base before accessing the field.
      ilg.Emit(OpCodes.Ldarg_0);
      ilg.Emit(OpCodes.Ldfld, fnum);

      // Load the instance of Base again, load the new value 
      // of Number, and store the new field value. 
      ilg.Emit(OpCodes.Ldarg_0);
      ilg.Emit(OpCodes.Ldarg_1);
      ilg.Emit(OpCodes.Stfld, fnum);

      // The original value of the Number field is now the only 
      // thing on the stack, so return from the call.
      ilg.Emit(OpCodes.Ret);


      // Create a delegate that uses changeNumber in the ordinary
      // way, as a static method that takes an instance of
      // Base and an int.
      //
      UseLikeStatic uls =
          (UseLikeStatic)changeNumber.CreateDelegate(
              typeof(UseLikeStatic)
          );

      // Create an instance of Base with a Number of 42.
      //
      Base ba = new Base(42);

      // Create a delegate that is bound to the instance of 
      // of Base. This is possible because the first 
      // parameter of changeNumber is of type Base. The 
      // delegate has all the parameters of changeNumber except
      // the first.
      UseLikeInstance uli =
          (UseLikeInstance)changeNumber.CreateDelegate(
              typeof(UseLikeInstance),
              ba
          );

      // First, change the value of Number by calling changeNumber as
      // a static method, passing in the instance of Base.
      //
      outputBlock.Text += String.Format(
          "Change the value of Number; previous value: {0}",
          uls(ba, 1492)
      ) + "\n";

      // Change the value of Number again using the delegate bound
      // to the instance of Base.
      //
      outputBlock.Text += String.Format(
          "Change the value of Number; previous value: {0}",
          uli(2700)
      ) + "\n";

      outputBlock.Text += String.Format("Final value of Number: {0}", ba.Number) + "\n";


      // Now repeat the process with a class that derives
      // from Base.
      //
      DerivedFromBase dfba = new DerivedFromBase(71);

      uli = (UseLikeInstance)changeNumber.CreateDelegate(
              typeof(UseLikeInstance),
              dfba
          );

      outputBlock.Text += String.Format(
          "Change the value of Number; previous value: {0}",
          uls(dfba, 73)
      ) + "\n";
      outputBlock.Text += String.Format(
          "Change the value of Number; previous value: {0}",
          uli(79)
      ) + "\n";
      outputBlock.Text += String.Format("Final value of Number: {0}", dfba.Number) + "\n";
   }
}

/* This example produces the following output:

Change the value of Number; previous value: 42
Change the value of Number; previous value: 1492
Final value of Number: 2700
Change the value of Number; previous value: 71
Change the value of Number; previous value: 73
Final value of Number: 79
 */


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

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

Community Additions

ADD
Show:
© 2014 Microsoft