Пример #1
0
        public void CreateDelegate_NoMethodBody_ThrowsInvalidOperationException()
        {
            IDClass       target = new IDClass();
            DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass));

            Assert.Throws <InvalidOperationException>(() => method.CreateDelegate(typeof(IntDelegate)));
            Assert.Throws <InvalidOperationException>(() => method.CreateDelegate(typeof(IntDelegate), target));
        }
Пример #2
0
        public void CreateDelegate_Type(IDClass target)
        {
            int newId = 0;

            FieldInfo     field       = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance);
            DynamicMethod method      = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, typeof(IDClass));
            ILGenerator   ilGenerator = method.GetILGenerator();

            Helpers.EmitMethodBody(ilGenerator, field);

            IDClassDelegate staticCallBack = (IDClassDelegate)method.CreateDelegate(typeof(IDClassDelegate));

            Assert.Equal(staticCallBack(target, newId), target.ID);
            Assert.Equal(newId, target.ID);
        }
        public void GetILGenerator_Int_Owner(bool skipVisibility)
        {
            IDClass   target = new IDClass();
            FieldInfo field  = typeof(IDClass).GetField(FieldName, BindingFlags.Instance | BindingFlags.NonPublic);

            Type[]        paramTypes = new Type[] { typeof(IDClass), typeof(int) };
            DynamicMethod method     = new DynamicMethod("Method", typeof(int), paramTypes, typeof(IDClass), skipVisibility);

            ILGenerator ilGenerator = method.GetILGenerator(8);

            Helpers.EmitMethodBody(ilGenerator, field);

            IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target);

            VerifyILGenerator(instanceCallBack, target, 0);
        }
Пример #4
0
        public void CreateDelegate_Target_Module(IDClass target)
        {
            Module module = typeof(TestClass).GetTypeInfo().Module;
            int    newId  = 0;

            FieldInfo     field  = typeof(IDClass).GetField(FieldName, BindingFlags.NonPublic | BindingFlags.Instance);
            DynamicMethod method = new DynamicMethod("Method", typeof(int), new Type[] { typeof(IDClass), typeof(int) }, module, true);

            ILGenerator ilGenerator = method.GetILGenerator();

            Helpers.EmitMethodBody(ilGenerator, field);

            IntDelegate instanceCallBack = (IntDelegate)method.CreateDelegate(typeof(IntDelegate), target);

            Assert.Equal(instanceCallBack(newId), target.ID);
            Assert.Equal(newId, target.ID);
        }
 private void VerifyILGenerator(IntDelegate instanceCallBack, IDClass target, int newId)
 {
     Assert.Equal(instanceCallBack(newId), target.ID);
     Assert.Equal(newId, target.ID);
 }