How do you harmony-patch a nested class?

Started by dninemfive, December 16, 2018, 11:02:55 PM

Previous topic - Next topic

dninemfive

I want to patch the method AddFreeWarmthAsNeeded from the class PossibleApparelSet, which is nested within PawnApparelGenerator. How do I target this? Visual Studio can't find any possible combination of those, e.g. PossibleApparelSet, PawnApparelGenerator.PossibleApparelSet, PawnApparelGenerator+PossibleApparelSet.

Incidentally, given how that method works, I'm going to need to change variables within that class. I do that by specifying them as "ref __name" in the parameters and working normally from there, right?

Mehni

harmony.Patch(AccessTools.Method(typeof(PawnApparelGenerator).GetNestedType("PossibleApparelSet", BindingFlags.NonPublic | BindingFlags.Instance), "AddFreeWarmthAsNeeded"), null, new HarmonyMethod(typeof(MyClass), nameof(MyPostFix)));

Something like that anyway, if you're using manual patching. If you use the [Annotations], you can use TargetMethod() or something -- check the harmony wiki.

I'm a bit rusty on targeting nested types, so what I said may not be 100% correct. It's still standard reflection, so at least it's a nudge in the right direction. You can look up the appropriate methods and BindingFlags required to target it on StackOverflow or the MSDN docs. "c# reflection get nested type" oughta get you far.

LWM

Using the targetmethod with annotation would look something like this:

https://github.com/pardeike/Harmony/issues/121

Except instead of .GenericMethod, you'd do the nested thing using reflection.  Good luck!