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 PosTest2() { 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_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); 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"); }