Exemple #1
0
        private void GeneratePrefixWithMultipleArgsWorks()
        {
            // Generate prefix for SingleArg
            MethodInfo method     = AccessTools.Method(typeof(Foo), nameof(Foo.ThreeArgs));
            MethodInfo dispatcher = AccessTools.Method(
                typeof(MethodPatchFactory_Test),
                nameof(DispatcherVoid));
            MethodAccess  access = new MethodAccess(method);
            DynamicMethod prefix = MethodPatchFactory.GeneratePrefix(
                access,
                dispatcher,
                EPatchBehaviour.AlwaysCallOriginal);

            // Call
            object ret = prefix.Invoke(m_Foo, new object[] { m_Foo, 42, 43.0f, 44.0 });

            Assert.IsType <bool>(ret);
            Assert.True((bool)ret);  // AlwaysCallOriginal
        }
Exemple #2
0
        private void ArgumentsAreForwarded()
        {
            // Generate prefix for ThreeArgs
            MethodInfo method     = AccessTools.Method(typeof(Foo), nameof(Foo.ThreeArgs));
            MethodInfo dispatcher = AccessTools.Method(
                typeof(MethodPatchFactory_Test),
                nameof(DispatcherCallOriginal));
            MethodAccess  access = new MethodAccess(method);
            DynamicMethod prefix = MethodPatchFactory.GeneratePrefix(
                access,
                dispatcher,
                EPatchBehaviour.NeverCallOriginal);

            // Call
            int    iArg = 42;
            float  fArg = 43.0f;
            double dArg = 44.0;
            object ret  = prefix.Invoke(m_Foo, new object[] { m_Foo, iArg, fArg, dArg });

            Assert.IsType <bool>(ret);
            Assert.False((bool)ret);  // NeverCallOriginal
            Assert.Single(m_DispatcherCalls);
            Assert.NotNull(m_DispatcherCalls[0]);
            Assert.Same(access, m_DispatcherCalls[0]);

            // Verify original was called
            Assert.Single(m_Foo.CallHistory);
            Assert.Equal(Foo.EMethod.ThreeArgs, m_Foo.CallHistory[0]);
            Assert.Single(m_Foo.ArgsHistory);
            Assert.Equal(3, m_Foo.ArgsHistory[0].Length);

            // Verify individual args where forwarded
            Assert.NotNull(m_Foo.ArgsHistory[0][0]);
            Assert.IsType <int>(m_Foo.ArgsHistory[0][0]);
            Assert.Equal(iArg, (int)m_Foo.ArgsHistory[0][0]);
            Assert.NotNull(m_Foo.ArgsHistory[0][1]);
            Assert.IsType <float>(m_Foo.ArgsHistory[0][1]);
            Assert.Equal(fArg, (float)m_Foo.ArgsHistory[0][1]);
            Assert.NotNull(m_Foo.ArgsHistory[0][2]);
            Assert.IsType <double>(m_Foo.ArgsHistory[0][2]);
            Assert.Equal(dArg, (double)m_Foo.ArgsHistory[0][2]);
        }
Exemple #3
0
        private void GeneratePrefixForVoidDispatcherWorks()
        {
            // Generate prefix for SingleArg
            MethodInfo method     = AccessTools.Method(typeof(Foo), nameof(Foo.SingleArg));
            MethodInfo dispatcher = AccessTools.Method(
                typeof(MethodPatchFactory_Test),
                nameof(DispatcherVoid));
            MethodAccess  access = new MethodAccess(method);
            DynamicMethod prefix = MethodPatchFactory.GeneratePrefix(
                access,
                dispatcher,
                EPatchBehaviour.AlwaysCallOriginal);

            // Call
            object ret = prefix.Invoke(m_Foo, new object[] { m_Foo, 42 });

            Assert.IsType <bool>(ret);
            Assert.True((bool)ret);  // AlwaysCallOriginal
            Assert.Single(m_DispatcherCalls);
            Assert.NotNull(m_DispatcherCalls[0]);
            Assert.Same(access, m_DispatcherCalls[0]);
        }