Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Reflection-FieldInfo.GetValue从另一个类获取变量和值_C#_Reflection_Getvalue - Fatal编程技术网

C#Reflection-FieldInfo.GetValue从另一个类获取变量和值

C#Reflection-FieldInfo.GetValue从另一个类获取变量和值,c#,reflection,getvalue,C#,Reflection,Getvalue,所以我尝试向调试输出中添加一些东西,目前我正在尝试动态获取类变量及其值。我尝试了一些东西,当我在同一个类中添加cod时,它运行得非常快,如下所示: var bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public; Console.WriteLine(""); foreach (var vari

所以我尝试向调试输出中添加一些东西,目前我正在尝试动态获取类变量及其值。我尝试了一些东西,当我在同一个类中添加cod时,它运行得非常快,如下所示:

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("");
try
{
    ...
}
catch (Exception ex)
{
    Debugger.Error(Context.Player.GetUsername(), ex, this);
}
这将输出以下结果:

15:47:09 [Test1] _currentTarget:
15:47:09 [Test1] _currentlyPathing: False
15:47:09 [Test1] _moveToTest:
15:47:09 [Test1] _botName: Test_ZerGo01
这正是我想要的,但当我试图将这些内容传递到实际的“调试器”输出时,我不能使用
这个
,因为它是一个静态方法。我不知道应该用什么替换
这个

这是我的“调试器”方法:


有人能告诉我我是做什么的吗?

您可以使用
null
作为
static
成员,而不是对象实例引用

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)

您需要将目标对象实例传递给
Error
方法,然后使用该实例代替
。请注意,您可以从对象实例获取类类型,因此不必传入。下面是修改后的方法的样子

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;
    }
    ...
}
然后,按如下方式调用该方法:

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("");
try
{
    ...
}
catch (Exception ex)
{
    Debugger.Error(Context.Player.GetUsername(), ex, this);
}

当我使用
null
时,我发现:
非静态字段需要一个目标。
您是否测试了
if(variable.IsStatic)
?同样感谢您,这从长远来看肯定会对我有所帮助。那么您要传递给它什么对象呢?在你问题顶部的代码中,你说它完全按照你想要的方式工作——它在哪个类中?您需要将该实例(
this
)传递给
Error
好吧,我不确定是否以正确的方式传递了该类,但这就是我所做的:
Debugger.Error(Context.Player.GetUsername(),e,typeof(TestingStuff))
我想从
TestingStuff
获取所有变量和值。您有一个
TestingStuff
实例,它包含变量,对吗?所以您应该做:
Debugger.Error(Context.Player.GetUsername(),e,testingStuffInstance)
如果从
TestingStuff
实例内部调用
Error
,那么它将是:
Debugger.Error(Context.Player.GetUsername(),e,this)我传递了错误的对象,这就是它不起作用的原因,谢谢。