代码中能否对类的某个方法临时改写?
测试代码如下:主程序实例化 ClsAAA,然后执行 aaa.ff() 这个方法。
class Program
{
static void Main(string[] args)
{
ClsAAA aaa = new ClsAAA();
//aaa.ff = ShowNothing;
aaa.ff();
Console.ReadKey();
}
private void ShowNothing()
{}
}
class ClsAAA
{
public void ff()
{
Console.WriteLine("OK");
}
}
请问有没有办法让 ff变为ShowNothing(),这样执行 aaa.ff() 实际是执行ShowNothing,不显示任何东西出来。
[解决办法]
class Program
{
static void Main(string[] args)
{
IClsAAA aaa = new ClsAAA();
//aaa.ff = ShowNothing;
aaa.ff();
Console.ReadKey();
}
}
interface IClsAAA
{
void ff();
}
class ClsAAA : IClsAAA
{
public void ff()
{
Console.WriteLine("OK");
}
}
//在测试代码中
public TestClsAAA : IClsAAA
{
public void ff()
{
ShowNothing();
}
}
private void ShowNothing()
{ }
public void TestClsAAA_ShowNothing()
{
IClsAAA aaa = new TestClsAAA();
aaa.ff();
}
[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)
{
if (setMethod == null
[解决办法]
getMethod == null) throw null;
methodPoint = getPoint(setMethod);
int* getMethodPoint = getPoint(getMethod);
if (methodPoint == getMethodPoint) throw null;
method = *methodPoint;
*methodPoint = *getMethodPoint;
}
protected abstract int* getPoint(methodType method);
public void Set(methodType method)
{
if (method == null) throw null;
*methodPoint = *getPoint(method);
}
public void Dispose()
{
if (method != 0)
{
*methodPoint = method;
method = 0;
}
}
}
public sealed unsafe class staticMethodWriter32<methodType> : methodWriterBase32<methodType>
{
public staticMethodWriter32(methodType setMethod, methodType getMethod) : base(setMethod, getMethod) { }
protected override int* getPoint(methodType method)
{
Delegate methodDelegate = method as Delegate;
if (!methodDelegate.Method.IsStatic) throw null;
unionMethod union = new unionMethod { Method = method };
return (int*)*((int*)*((int*)union.Point.Point + 4) + 2) + 2;
}
}
public sealed unsafe class methodWriter32<methodType> : methodWriterBase32<methodType>
{
public methodWriter32(methodType setMethod, methodType getMethod) : base(setMethod, getMethod) { }
protected override int* getPoint(methodType method)
{
Delegate methodDelegate = method as Delegate;
if (methodDelegate.Method.IsStatic) throw null;
unionMethod union = new unionMethod { Method = method };
int point = union.Point.Point;
int* methodPoint = (int*)point;
while (*methodPoint != point) ++methodPoint;
return (int*)*((int*)*(methodPoint + 2) + 2) + 2;
}
}
class ClsAAA
{
public void ff()
{
Console.WriteLine("OK");
}
}
class ClsBBB
{
public void ff()
{
}
} static void Main(string[] args)
{
ClsAAA aaa = new ClsAAA();
using (methodWriter32<Action> method = new methodWriter32<Action>(aaa.ff, new ClsBBB().ff))
{
aaa.ff();
}
Console.ReadKey();
}