Reflect variables?

Started by CallMeDio, August 09, 2016, 08:12:41 PM

Previous topic - Next topic

CallMeDio

Hello, Its me again with my problematic detours, I need to detour a method that reads and writes to a private variable of its class, do anyone have a example of how I can reflect variables?
Just a generic example or point me to a source code that haves it
QuoteYou may need a rubber duck.  Also, try some caveman debugging.

Released mods: No Mood Loss on Prisoner Sold or Died

CallMeDio

#1
Managed to do this. Not all this different from reflecting methods and fields...
example reflecting the private Pawn pawn from Pawn_AgeTracker. Still didn't tested. but no errors on compile, so I guess thats it.

         internal static PropertyInfo _pawn;

         internal static Pawn pawn()
         {
            if (_pawn == null)
            {
               _pawn = typeof(Pawn_AgeTracker).GetProperty("pawn", BindingFlags.Instance | BindingFlags.NonPublic);
            }
            return (Pawn)_pawn.GetValue(_pawn,null);
         }


QuoteYou may need a rubber duck.  Also, try some caveman debugging.

Released mods: No Mood Loss on Prisoner Sold or Died

Jaxxa

This Might Help. I have Method that takes a Projectile and then uses Reflection to return its private destination variable. Not that I have tried it but it looks like there is also a SetValue method on the fieldInfo that should work for setting it. This is some of the earlier reflection code that I so it might not be that good, but it works and hopefully it can be of some help.

Have a look at the "GetTargetLocationFromProjectile" Method in https://github.com/jaxxa/ED-Shields/blob/master/Source/ED-Shields/Shields/Building_Shield.cs

CallMeDio

Hey, I was about to ask how to write to it (as Im messing with .SetValue without success) as I saw your message. I will take a look
QuoteYou may need a rubber duck.  Also, try some caveman debugging.

Released mods: No Mood Loss on Prisoner Sold or Died

CallMeDio

#4
Wow, nice! never saw a mod file with 1k lines  :o,
I searched all the files and there is no .SetValue gonna keep fighting

edit: apparently I did it

_pawn.SetValue(_pawn, valuehere, null);
QuoteYou may need a rubber duck.  Also, try some caveman debugging.

Released mods: No Mood Loss on Prisoner Sold or Died

1000101

#5
Quote from: CallMeDio on August 09, 2016, 08:41:39 PMexample reflecting the private Pawn pawn from Pawn_AgeTracker. Still didn't tested. but no errors on compile, so I guess thats it.
         internal static PropertyInfo _pawn;

         internal static Pawn pawn()
         {
            if (_pawn == null)
            {
               _pawn = typeof(Pawn_AgeTracker).GetProperty("pawn", BindingFlags.Instance | BindingFlags.NonPublic);
            }
            return (Pawn)_pawn.GetValue(_pawn,null);
         }


That code will throw an error and won't work.  You are passing the PropertyInfo as the Object to get the Property Value from.  Further, "pawn" is not a property of Pawn_AgeTracker, it's a field so you are reflecting a FieldInfo.

Try this instead:
         internal static FieldInfo _pawn;

         internal static Pawn GetPawn( this Pawn_AgeTracker _this )
         {
            if (_pawn == null)
            {
               _pawn = typeof(Pawn_AgeTracker).GetField("pawn", BindingFlags.Instance | BindingFlags.NonPublic);
            }
            return (Pawn)_pawn.GetValue(_this,null);
         }

(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

CallMeDio

I just noticed my errors now you said it as in the end I figured I was detouring the wrong thing and didn't needed this part. But thanks for noticing it as I could have confused someone looking on this thread for help  :P

QuoteYou may need a rubber duck.  Also, try some caveman debugging.

Released mods: No Mood Loss on Prisoner Sold or Died

CallMeDio

#7
Is there anyway to do that when the class that have the thing you want to reflect is static? just spent the last hours detouring and reflecting a massive method to get a crapton of errors that the reflections I did are not set to a instance of an object. But if I add the class I get other errors, that a static class cannot be used like that...

Edit: Nvm, just found out (the thing in the example is not in a static class, but Im just doing like it was on the same example to show the difference):

internal static FieldInfo _pawn;

         internal static Pawn GetPawn()
         {
            if (_pawn == null)
            {
               _pawn = typeof(Pawn_AgeTracker).GetField("pawn", BindingFlags.Instance | BindingFlags.NonPublic);
            }
            return (Pawn)_pawn.GetValue(null);
         }
QuoteYou may need a rubber duck.  Also, try some caveman debugging.

Released mods: No Mood Loss on Prisoner Sold or Died

1000101

Still wrong.  The code I posted was correct.
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By

CallMeDio

It worked anyways (it is for reflecting stuff that is inside static classes where you can't do: "this blablaclass _this"
QuoteYou may need a rubber duck.  Also, try some caveman debugging.

Released mods: No Mood Loss on Prisoner Sold or Died

1000101

Doesn't matter, the code you posted is wrong.  That it's irrelevant is a side issue.  :P
(2*b)||!(2*b) - That is the question.
There are 10 kinds of people in this world - those that understand binary and those that don't.

Powered By