Exemplo n.º 1
0
        public void Test_GenericsOriginalMethod()
        {
            var originalMethod = typeof(Class13 <int>).GetMethod(nameof(Class13 <int> .Add));

            Assert.NotNull(originalMethod);

            var patchClass = typeof(Class13Patch);
            var prefix     = patchClass.GetMethod("Prefix");

            Assert.NotNull(prefix);

            var list1 = new Class13 <int> {
                1, 2, 3
            };

            list1.Add(1000);
            var e1 = list1.GetEnumerator();

            _ = e1.MoveNext();
            _ = e1.MoveNext();
            _ = e1.MoveNext();
            _ = e1.MoveNext();
            Assert.AreEqual(1000, e1.Current);

            var instance = new Harmony("test");

            Assert.NotNull(instance);

            var patcher = instance.CreateProcessor(originalMethod);

            Assert.NotNull(patcher);
            _ = patcher.AddPrefix(prefix);
            _ = patcher.Patch();

            Class13Patch.method = null;
            Class13Patch.result = 0;

            var list2 = new Class13 <int> {
                1, 2, 3
            };

            list2.Add(1000);
            var e2 = list2.GetEnumerator();

            _ = e2.MoveNext();
            _ = e2.MoveNext();
            _ = e2.MoveNext();
            _ = e2.MoveNext();
            Assert.AreEqual(999, e2.Current);

            Assert.AreEqual(1000, Class13Patch.result);
            Assert.AreEqual(originalMethod, Class13Patch.method);
        }