日期:2014-05-17  浏览次数:20971 次

对类的某个方法临时改写的代码排错
在我询问 “代码中能否对类的某个方法临时改写”,sbwwkmyd 给出其中一种方案的代码,
using System;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            ClsAAA aaa = new ClsAAA();

            using (methodWriter32<Action> method = new methodWriter32<Action>(aaa.ff, new Program().nothing)) 
            {
                aaa.ff();
            }
            Console.ReadKey();
        }

        public void nothing()
        {
        }
    }

    class ClsAAA
    {
        public void ff()
        {
            Console.WriteLine("OK");
        }
    }

    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
    internal struct unionMethod
    {
        internal class point
        {
            public int Point;
        }

        [System.Runtime.InteropServices.FieldOffset(0)]
        public point Point;
        [System.Runtime.InteropServices.FieldOffset(0)]
        public object Method;
    }

    internal unsafe struct methodTarget
    {
        public int* MethodPoint;
        public object Target;
        public int* TargetPoint;
    }

    internal class targetPoint
    {
        public object Target;
    }

    public unsafe abstract class methodWriterBase32<methodType> : IDisposable
    {
        private int* methodPoint;
        private int method;
        public methodWriterBase32(methodType setMethod, methodType getMethod)
        {
  &nbs