C# 单元测试 harmony 补丁
C# Harmony 单元测试
·
harmony 做单元测试,打补丁非常灵活,可以绕过很多无法测试的代码
Harmony 方法补丁
Public、Privite 方法都可以进行补丁
[HarmonyPatch(typeof(MyClass))]
[HarmonyPatch(nameof(MyClass.Method))]
public class HookMyClass
{
/// <summary>
/// 前置拦截方法
/// </summary>
/// <param name="__result"></param>
/// <returns></returns>
[HarmonyPrefix]
public static bool Prefix(ref string __result)
{
//这里可以自定义处理result
//false不调用原方法,ture调用原方法
return false;
}
/// <summary>
/// 补丁方法
/// </summary>
/// <param name="__result"></param>
[HarmonyPostfix]
public static void Postfix(ref string __result)
{
}
/// <summary>
/// 后置方法
/// </summary>
/// <param name="__result"></param>
[HarmonyFinalizer]
public static void Finalizer()
{
}
}
属性补丁
[HarmonyPatch(typeof(MyClass), "MyProperty")]
public class MyClassPatch
{
public static MethodInfo Original;
[HarmonyPatch(typeof(MyClass), "MyProperty", MethodType.Getter)]
[HarmonyPrefix]
public static bool Prefix(ref string __result)
{
//Debug.Log("MyProperty: " + __result);
__result = "替换get到的值";
return false;
}
public static void Patch(Harmony harmony)
{
Original = AccessTools.PropertyGetter(typeof(MyClass), "MyProperty");
harmony.Patch(Original, prefix: new HarmonyMethod(typeof(MyClassPatch), "Prefix"));
}
}
更多推荐
所有评论(0)