public void NegTest1() { DynamicMethod testDynMethod; Type retType; Type[] paramTypes; TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1(); retType = typeof(int); paramTypes = new Type[] { _DYNAMIC_METHOD_OWNER_TYPE, typeof(int) }; testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, retType, paramTypes, _DYNAMIC_METHOD_OWNER_TYPE); Assert.Throws <InvalidOperationException>(() => { UseLikeInstance instanceCallBack = (UseLikeInstance)testDynMethod.CreateDelegate(typeof(UseLikeInstance), target); }); }
public void PosTest4() { DynamicMethod testDynMethod; Type retType; Type[] paramTypes; FieldInfo fieldInfo; TestCreateDelegateOwner1Derived target = new TestCreateDelegateOwner1Derived(); int newId = 100; bool actualResult; retType = typeof(int); paramTypes = new Type[] { _DYNAMIC_METHOD_OWNER_TYPE, typeof(int) }; fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( c_FIELD_NAME, BindingFlags.NonPublic | BindingFlags.Instance); testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, retType, paramTypes, _DYNAMIC_METHOD_OWNER_DERIVED_TYPE, true); ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); testDynMethodIL.Emit(OpCodes.Ldarg_0); testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); testDynMethodIL.Emit(OpCodes.Ldarg_0); testDynMethodIL.Emit(OpCodes.Ldarg_1); testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); testDynMethodIL.Emit(OpCodes.Ret); UseLikeInstance instanceCallBack = (UseLikeInstance)testDynMethod.CreateDelegate( typeof(UseLikeInstance), target); actualResult = target.ID == instanceCallBack(newId); actualResult = (target.ID == newId) && actualResult; Assert.True(actualResult, "Failed to create delegate for dynamic method."); }
public void NegTest2() { DynamicMethod testDynMethod; Type retType; Type[] paramTypes; FieldInfo fieldInfo; string target = "foo"; retType = typeof(int); paramTypes = new Type[] { _DYNAMIC_METHOD_OWNER_TYPE, typeof(int) }; fieldInfo = _DYNAMIC_METHOD_OWNER_TYPE.GetField( c_FIELD_NAME, BindingFlags.NonPublic | BindingFlags.Instance); testDynMethod = new DynamicMethod(c_DYNAMIC_METHOD_NAME, retType, paramTypes, _DYNAMIC_METHOD_OWNER_TYPE); ILGenerator testDynMethodIL = testDynMethod.GetILGenerator(); testDynMethodIL.Emit(OpCodes.Ldarg_0); testDynMethodIL.Emit(OpCodes.Ldfld, fieldInfo); testDynMethodIL.Emit(OpCodes.Ldarg_0); testDynMethodIL.Emit(OpCodes.Ldarg_1); testDynMethodIL.Emit(OpCodes.Stfld, fieldInfo); testDynMethodIL.Emit(OpCodes.Ret); Assert.Throws <ArgumentException>(() => { UseLikeInstance instanceCallBack = (UseLikeInstance)testDynMethod.CreateDelegate(typeof(UseLikeInstance), target); }); }
public static void Main() { // This dynamic method changes the private id field. It has // no name; it returns the old id value (return type int); // it takes two parameters, an instance of Example and // an int that is the new value of id; and it is declared // with Example as the owner type, so it can access all // members, public and private. // DynamicMethod changeID = new DynamicMethod( "", typeof(int), new Type[] { typeof(Example), typeof(int) }, typeof(Example) ); // Get a FieldInfo for the private field 'id'. FieldInfo fid = typeof(Example).GetField( "id", BindingFlags.NonPublic | BindingFlags.Instance ); ILGenerator ilg = changeID.GetILGenerator(); // Push the current value of the id field onto the // evaluation stack. It's an instance field, so load the // instance of Example before accessing the field. ilg.Emit(OpCodes.Ldarg_0); ilg.Emit(OpCodes.Ldfld, fid); // Load the instance of Example again, load the new value // of id, and store the new field value. ilg.Emit(OpCodes.Ldarg_0); ilg.Emit(OpCodes.Ldarg_1); ilg.Emit(OpCodes.Stfld, fid); // The original value of the id field is now the only // thing on the stack, so return from the call. ilg.Emit(OpCodes.Ret); // Create a delegate that uses changeID in the ordinary // way, as a static method that takes an instance of // Example and an int. // UseLikeStatic uls = (UseLikeStatic)changeID.CreateDelegate( typeof(UseLikeStatic) ); // Create an instance of Example with an id of 42. // Example ex = new Example(42); // Create a delegate that is bound to the instance of // of Example. This is possible because the first // parameter of changeID is of type Example. The // delegate has all the parameters of changeID except // the first. UseLikeInstance uli = (UseLikeInstance)changeID.CreateDelegate( typeof(UseLikeInstance), ex ); // First, change the value of id by calling changeID as // a static method, passing in the instance of Example. // Console.WriteLine( "Change the value of id; previous value: {0}", uls(ex, 1492) ); // Change the value of id again using the delegate bound // to the instance of Example. // Console.WriteLine( "Change the value of id; previous value: {0}", uli(2700) ); Console.WriteLine("Final value of id: {0}", ex.ID); // Now repeat the process with a class that derives // from Example. // DerivedFromExample dfex = new DerivedFromExample(71); uli = (UseLikeInstance)changeID.CreateDelegate( typeof(UseLikeInstance), dfex ); Console.WriteLine( "Change the value of id; previous value: {0}", uls(dfex, 73) ); Console.WriteLine( "Change the value of id; previous value: {0}", uli(79) ); Console.WriteLine("Final value of id: {0}", dfex.ID); }