public void AnonHosted() { DynamicMethod hello = new DynamicMethod("Hello", typeof(int), new Type[] { typeof(string) }); ILGenerator helloIL = hello.GetILGenerator(); helloIL.Emit(OpCodes.Ldc_I4_2); helloIL.Emit(OpCodes.Ret); HelloInvoker hi = (HelloInvoker)hello.CreateDelegate(typeof(HelloInvoker)); int ret = hi("Hello, World!"); Assert.AreEqual(2, ret); object[] invokeArgs = { "Hello, World!" }; object objRet = hello.Invoke(null, invokeArgs); Assert.AreEqual(2, objRet); }
[Test] // bug #78253 public void DynamicMethodReference() { DynamicMethod hello = new DynamicMethod("Hello", typeof(int), new Type[] { typeof(string) }, typeof(DynamicMethodTest).Module); Assert.IsNull(hello.DeclaringType, "#1"); DynamicMethod write = new DynamicMethod("Write", typeof(int), new Type[] { typeof(string) }, typeof(DynamicMethodTest)); Assert.IsNull(hello.DeclaringType, "#2"); MethodInfo invokeWrite = write.GetBaseDefinition(); ILGenerator helloIL = hello.GetILGenerator(); helloIL.Emit(OpCodes.Ldarg_0); helloIL.EmitCall(OpCodes.Call, invokeWrite, null); helloIL.Emit(OpCodes.Ret); ILGenerator writeIL = write.GetILGenerator(); writeIL.Emit(OpCodes.Ldc_I4_2); writeIL.Emit(OpCodes.Ret); HelloInvoker hi = (HelloInvoker)hello.CreateDelegate(typeof(HelloInvoker)); int ret = hi("Hello, World!"); Assert.AreEqual(2, ret, "#3"); object[] invokeArgs = { "Hello, World!" }; object objRet = hello.Invoke(null, invokeArgs); Assert.AreEqual(2, objRet, "#4"); }
public static void Main() { // Create an array that specifies the types of the parameters // of the dynamic method. This method has a string parameter // and an int parameter. Type[] helloArgs = { typeof(string), typeof(int) }; // Create a dynamic method with the name "Hello", a return type // of int, and two parameters whose types are specified by the // array helloArgs. Create the method in the module that // defines the Test class. DynamicMethod hello = new DynamicMethod("Hello", typeof(int), helloArgs, typeof(Test).Module); // <Snippet2> // Create an array that specifies the parameter types of the // overload of Console.WriteLine to be used in Hello. Type[] writeStringArgs = { typeof(string) }; // Get the overload of Console.WriteLine that has one // String parameter. MethodInfo writeString = typeof(Console).GetMethod("WriteLine", writeStringArgs); // Get an ILGenerator and emit a body for the dynamic method. ILGenerator il = hello.GetILGenerator(); // Load the first argument, which is a string, onto the stack. il.Emit(OpCodes.Ldarg_0); // Call the overload of Console.WriteLine that prints a string. il.EmitCall(OpCodes.Call, writeString, null); // The Hello method returns the value of the second argument; // to do this, load the onto the stack and return. il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Ret); // </Snippet2> // <Snippet3> // Create a delegate that represents the dynamic method. This // action completes the method, and any further attempts to // change the method will cause an exception. HelloInvoker hi = (HelloInvoker)hello.CreateDelegate(typeof(HelloInvoker)); // </Snippet3> // Use the delegate to execute the dynamic method. Save and // print the return value. int retval = hi("\r\nHello, World!", 42); Console.WriteLine("Executing delegate hi(\"Hello, World!\", 42) returned {0}", retval); // Do it again, with different arguments. retval = hi("\r\nHi, Mom!", 5280); Console.WriteLine("Executing delegate hi(\"Hi, Mom!\", 5280) returned {0}", retval); // <Snippet4> // Create an array of arguments to use with the Invoke method. object[] invokeArgs = { "\r\nHello, World!", 42 }; // Invoke the dynamic method using the arguments. This is much // slower than using the delegate, because you must create an // array to contain the arguments, and ValueType arguments // must be boxed. object objRet = hello.Invoke(null, invokeArgs); Console.WriteLine("hello.Invoke returned {0}", objRet); // </Snippet4> }