Example #1
0
        public void TestMethod2()
        {
            //Arrange

            Action method = () =>
            {
                Int32 a = 5;
                try
                {
                    a = 6;
                }
                finally
                {
                    a = 7;
                }
            };

            //Act
            MethodLoader loader  = new MethodLoader(method.GetMethodInfo());
            IEHTable     ehTable = loader.EHTable;

            //Assert
            var handlers = ehTable.GetAllHandlers();

            Assert.AreEqual(14, loader.ILInstructions.Count);
            Assert.AreEqual(1, handlers.Count);
            Assert.AreEqual(3, handlers[0].TryOffset);
            Assert.AreEqual(4, handlers[0].TryLength);
            Assert.AreEqual(8, handlers[0].HandlerOffset);
            Assert.AreEqual(4, handlers[0].HandlerLength);
            Assert.AreEqual(EHHandlerType.Finally, handlers[0].HandlerType);
            Assert.AreEqual(null, handlers[0].ExceptionToken);
        }
Example #2
0
        public void TestMethod3()
        {
            //Arrange
            Action method = () =>
            {
                Int32 a = 5;
                try
                {
                    a = 6;
                    try
                    {
                        a = 9;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception in try");
                    }
                }
                finally
                {
                    try
                    {
                        a = 7;
                    }
                    catch (Exception ex)
                    {
                        a = 11;
                        Console.WriteLine("Exception in finally");
                    }
                }
            };

            //Act
            MethodLoader loader  = new MethodLoader(method.GetMethodInfo());
            IEHTable     ehTable = loader.EHTable;

            //Assert
            Assert.AreEqual(38, loader.ILInstructions.Count);
            var handlers = ehTable.GetAllHandlers();

            Assert.AreEqual(3, handlers.Count);

            Assert.AreEqual(6, handlers[0].TryOffset);
            Assert.AreEqual(4, handlers[0].TryLength);
            Assert.AreEqual(11, handlers[0].HandlerOffset);
            Assert.AreEqual(6, handlers[0].HandlerLength);
            Assert.AreEqual(EHHandlerType.Catch, handlers[0].HandlerType);
            Assert.IsTrue(handlers[0].ExceptionToken.Equals(new ClassToken(typeof(System.Exception))));


            Assert.AreEqual(21, handlers[1].TryOffset);
            Assert.AreEqual(4, handlers[1].TryLength);
            Assert.AreEqual(26, handlers[1].HandlerOffset);
            Assert.AreEqual(8, handlers[1].HandlerLength);
            Assert.AreEqual(EHHandlerType.Catch, handlers[1].HandlerType);
            Assert.IsTrue(handlers[1].ExceptionToken.Equals(new ClassToken(typeof(System.Exception))));


            Assert.AreEqual(3, handlers[2].TryOffset);
            Assert.AreEqual(16, handlers[2].TryLength);
            Assert.AreEqual(20, handlers[2].HandlerOffset);
            Assert.AreEqual(16, handlers[2].HandlerLength);
            Assert.AreEqual(EHHandlerType.Finally, handlers[2].HandlerType);
            Assert.AreEqual(null, handlers[2].ExceptionToken);
        }
Example #3
0
 public MethodDesc AddEHTable(IEHTable ehTable)
 {
     this.EHTable = ehTable;
     return(this);
 }