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 PosTest3()
        {
            DynamicMethod testDynMethod;
            Type          retType;

            Type[]    paramTypes;
            FieldInfo fieldInfo;
            TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1();
            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,
                                              this.CurrentModule,
                                              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 PosTest1()
        {
            DynamicMethod testDynMethod;
            Type retType;
            Type[] paramTypes;
            FieldInfo fieldInfo;
            TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1();
            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.");
        }
        public void NegTest3()
        {
            DynamicMethod testDynMethod;
            Type          retType;

            Type[]    paramTypes;
            FieldInfo fieldInfo;
            TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1();

            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>(() => { InvalidRetType invalidCallBack = (InvalidRetType)testDynMethod.CreateDelegate(typeof(InvalidRetType), target); });
        }
        public void NegTest5()
        {
            DynamicMethod testDynMethod;
            Type retType;
            Type[] paramTypes;
            FieldInfo fieldInfo;
            TestCreateDelegateOwner1 target = new TestCreateDelegateOwner1();

            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>(() => { InvalidParamType invalidCallBack = (InvalidParamType)testDynMethod.CreateDelegate(typeof(InvalidParamType), target); });
        }
        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);
            });
        }