簡體   English   中英

C# 反射 - FieldInfo.GetValue 從另一個 class 獲取變量和值

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

所以我試圖在我的調試 output 中添加一些東西,我目前正在嘗試動態獲取 class 變量及其值。 當我在相同的 class 中添加代碼時,我嘗試了一些東西並讓它工作得非常快,如下所示:

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("");

這將輸出以下結果:

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

這正是我想要的,但是當我嘗試將這些東西傳遞給我的實際“調試器”output 時,我無法使用this ,因為它是 static 方法。 我不知道應該用什么替換this

這是我的“調試器”方法:

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;
    }
    ...
}

有人可以告訴我我在做什么嗎?

您可以將null用於static成員,而不是 object 實例引用。

if ( variable.IsStatic ) 
  message += DebuggerLine(variable.Name, variable.GetValue(null).ToString()) + nL;
else
if ( instance != null )
  message += DebuggerLine(variable.Name, variable.GetValue(instance).ToString()) + nL;
else
  // Manage method call mistmatch: 
  // add an error string to the message or do nothing or what you want

修改方法簽名為:

public static void Error(string playerName,
                         Exception ex,
                         Type classType = null,
                         object instance = null)

https://docs.microsoft.com/dotnet/api/system.reflection.fieldinfo.getvalue

您需要將目標 object 實例傳遞給您的Error方法,然后使用該實例代替this 請注意,您可以從 object 實例中獲取 class 類型,因此您不必將其傳入。這是修改后的方法的樣子。

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

        Type classType = instance.GetType();
        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(instance).ToString()) + nL;
            }
        }

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

然后,像這樣調用方法:

try
{
    ...
}
catch (Exception ex)
{
    Debugger.Error(Context.Player.GetUsername(), ex, this);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM