简体   繁体   English

C#:FieldInfo.GetValue返回null

[英]C# : FieldInfo.GetValue returns null

I've a problem to retrieve my control f2 in the variable o via Reflection : 我有一个问题需要通过Reflection检索变量o中的控件f2:

public partial class Form1 : Form
{
    private Form2 f2;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Type controlType = this.GetType();
        FieldInfo f = controlType.GetField("f2", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        object o = f.GetValue(this); // o == null;
    }
}

That's because you create a local variable called f2 in button1_Click but never set the class member f2 to that new instance: 那是因为您在button1_Click创建了一个名为f2的局部变量,但从未将类成员f2设置为该新实例:

private void button1_Click(object sender, EventArgs e)
{
    // Creates a new variable called f2 that is local to the function
    Form2 f2 = new Form2();

    // To store the local instance to the class member, you need to un-comment
    // this.f2 = f2;        
    // or change the previous line of code to:
    // f2 = new Form2();

    // Show the local form
    f2.Show();
}

Therefore there is a null value in the class level f2 . 因此,在类级别f2存在空值。

I'm also assuming that you're just playing with reflection in this example. 我还假设您在此示例中只是在玩反射。 If this isn't strictly a test of reflection...you should just reference this.f2 directly rather than through reflection. 如果这不是严格的反射测试,则应直接引用this.f2而不是通过反射。

FieldInfo myf = typeof(Form1).GetField("f2");
   Console.Writeline(myf.GetValue(this));

Should work for ya! 应该为你工作!

You aren't setting the value of the private field f2. 您没有设置私有字段f2的值。 Instead you are creating a local variable called f2. 相反,您正在创建一个名为f2的局部变量。 Instead of this code: 代替此代码:

Form f2 = new Form();

use: 采用:

f2 = new Form();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 c# 中的 FieldInfo.GetValue 动态转换对象 - Dynamically casting objects from FieldInfo.GetValue in c# FieldInfo.GetValue() 为嵌套的静态类公共静态字段返回 null - FieldInfo.GetValue() returns null for a nested static class public static fields C# 反射 - FieldInfo.GetValue 从另一个 class 获取变量和值 - C# Reflection - FieldInfo.GetValue get variables and values from another class 为什么FieldInfo.GetValue(null)在静态构造函数中不起作用 - Why is FieldInfo.GetValue(null) not working in static constructor 如何缓存 FieldInfo.GetValue()? - How do I cache FieldInfo.GetValue()? 如何加快FieldInfo.GetValue()的调用? - How to speed up FieldInfo.GetValue() calls? FieldInfo.GetValue为私有成员返回null,而调试器指示字段为非null? - FieldInfo.GetValue return null for a private member while debugger indicates field is non null? 如何将当前实例传递给实例内部的FieldInfo.GetValue? - How to pass the current instance to FieldInfo.GetValue inside the instance? 从FieldInfo.GetValue返回的对象不能强制转换为Array - Object returned from FieldInfo.GetValue cannot be cast to Array 如何自动将从FieldInfo.GetValue获得的值转换为正确的类型? - How do I automatically cast a value obtained from FieldInfo.GetValue to the correct type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM