Ludeon Forums

RimWorld => Mods => Help => Topic started by: arkyte on May 07, 2021, 10:45:17 AM

Title: How to call an internal/private method of a class and use it's returned values?
Post by: arkyte on May 07, 2021, 10:45:17 AM
I solved it, solution for the future on the botton

I have a Pawn_DraftController instance __instance.drafter that has an internal method __instance.drafter.GetGizmos() which does several things and yields an IEnumerable<Gizmo>.

I searched in the forums and found that I could use

Traverse.Create(__instance.drafter).Field("value").GetValue<type>();

but I want to call the method and not get a private value.

Thanks in advance.

Edit:

I tried to

IEnumerable<Gizmo> gizmos_drafter = Traverse.Create(__instance.drafter).Method("GetGizmos", null).GetValue<IEnumerable<Gizmo>>();

I think it didn't work

Solved:

I managed to get something working

var getGizmosMethod = __instance.drafter.GetType().GetMethod("GetGizmos", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

IEnumerable<Gizmo> gizmos_drafter = (IEnumerable<Gizmo>) getGizmosMethod.Invoke(__instance.drafter, null);