dimanche 27 octobre 2019

C# Reflection - FieldInfo.GetValue get variables and values from another class

so I'm trying to add some stuff to my debug output and I'm currently trying to dynamically get the class variables and the values of it. I tried some stuff and got it working quite fast when I added the cod ein the same class like this:

var bindingFlags = BindingFlags.Instance |
        BindingFlags.Static |
        BindingFlags.NonPublic |
        BindingFlags.Public;

Console.WriteLine("");
foreach (var variable in typeof(TestingStuff).GetFields(bindingFlags))
{
    Debugger.Debug(Context.Player.GetUsername(), $"{variable.Name}: variable.GetValue(this)}");
}
Console.WriteLine("");

This outputs the following result:

15:47:09 [Test1] _currentTarget:
15:47:09 [Test1] _currentlyPathing: False
15:47:09 [Test1] _moveToTest:
15:47:09 [Test1] _botName: Test_ZerGo01

This is exactly what I want, but when I try to pass that stuff to my actual "Debugger" output I can't use this because it's a static method. I have no idea what I should replace the this with.

This is my "debugger" method:

public static void Error(string playerName, Exception ex, Type classType = null)
{
    ...
    if (classType != null)
    {
        var bindingFlags = BindingFlags.Instance |
            BindingFlags.Static |
            BindingFlags.NonPublic |
            BindingFlags.Public;

        if (classType.GetFields(bindingFlags).Length > 1)
        {
            message += DebuggerLine("Plugin Class Variables") + nL;

            foreach (var variable in classType.GetFields(bindingFlags))
            {
                message += DebuggerLine(variable.Name, variable.GetValue(???).ToString()) + nL;
            }
        }

        message += DebuggerLine() + nL;
    }
    ...
}

Could someone please tell me what I do?





Aucun commentaire:

Enregistrer un commentaire