The reflect helper allows developers to directly access private fields and private types.

If you need to use this feature, please add a reference to the Fody package.

How to use

Use <ReflectHelper Include="<AssemblyName>" /> to indicate which assemblies need to construct Reflect Helper.

<AssemblyName> can use assemblies explicitly referenced in project files using <Reference> and MAPI assemblies and other Mods assemblies inserted by HKBuildUtils.

For example:

<ReflectHelper Include="UnityEngine.CoreModule" />
<ReflectHelper Include="Assembly-CSharp" />

They indicate the Reflect Helper that generates UnityEngine.CoreModule and Assembly-CSharp

They can be used like this:

HeroControllerR reflect = HeroController.instance.Reflect();
HeroController orig = reflect.ToOriginal();
HeroControllerR reflect = (HeroControllerR)(object)HeroController.instance;
HeroController orig = (HeroController)(object)reflect;
HeroControllerR reflect = HeroControllerR.instance;
ModLoaderR.TryAddModInstance(typeof(TestModMod), new ModInstanceR()
{
   Enabled = true,
   Mod = this,
   Name = "Hello, World!This is Test Mod"
}); 
List<ModInstanceR> allMods = ModLoaderR.ModInstances.ToList();
foreach(var mod in allMods)
{
   Log($"Mod Name: {mod.Name}");
}

Compare with HKMirror

Advantages:

  • No need to add additional references.
  • Can be used with almost any assembly.
  • Has most of the private types.
  • Custom operator operations with primitive types.
  • Primitive object instances can be created directly using new. For example, new ModInstanceR() will create an instance of ModLoader.ModInstance.
  • Supports generics.

Defects:

  • No quick access to singleton objects.
  • Does not carry HookHelper.

Comparison to HKReflect

Advantages:

  • Can be used with almost any assembly.
  • Has most of the private types.
  • Can use reflect type to completely replace original type

Defects:

  • No quick access to singleton objects.