accessing an internal / private method / field.

Started by Fluffy (l2032), September 13, 2015, 05:19:44 PM

Previous topic - Next topic

Fluffy (l2032)

I would like to access a pawn (animals) currently completed training steps. The code used in core is in Pawn_TrainingTracker.GetSteps(TrainableDef), however this method is marked internal, the steps field used to track training is marked private.

Any ideas on how to access these? I've fiddled with reflection but can't get it to work (probably my lack of expertise), and I'ld like to avoid using a custom Pawn class and rewriting and overriding all the training code...

P.S. Tynan, why is this method marked internal to begin with? All the other methods in the class are public...

RawCode

you can access rimworld private fields just like any other private field via c# reflection.

there is nothing special about rimworld

Fluffy (l2032)

Could you give me an example of how to do this? Or a link to a mod that does this? I tried the various GetAssembly methods, but they either return my own assembly or null, which was not a promising start...

Latta

As long as you specify BindingFlags of both NonPublic and Instance or Static, internal(or private, or protected) methods can be invoked.

var foo = SomeClass.GetType().GetMethod("MethodName", BindingFlags.NonPublic | BindingFlags.Instance(or Static));
foo.Invoke(something something);