Base33
Base33

Reputation: 3215

Type.GetFields returns empty array

I am trying to get public fields dynamically but it keeps returning an empty list. I load a usercontrol successfully but it is of type UserControl which means I use .BaseType to get the real type. But when I call .GetFields() it returns an empty FieldInfo array.

usercontrolPath = "/usercontrols/HelloWorldTestUC.ascx"
Page pageHolder = new Page();
UserControl usercontrol = (UserControl)pageHolder.LoadControl("~/" + usercontrolPath);
Type type = usercontrol.GetType().BaseType;

FieldInfo[] infos = type.GetFields(BindingFlags.Public);

//i will do something here
control.Controls.Add(usercontrol);

Here is the UserControl code:

public partial class HelloWorldTestUC : System.Web.UI.UserControl
{
    public int Number = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtMessage.Text = Message.ToString();
        }
    }
}

Any idea why it would return an empty array? It should return 'Number' at the very least.

Upvotes: 0

Views: 1789

Answers (1)

vcsjones
vcsjones

Reputation: 141638

Your BindingFlags are incomplete. You probably want BindingFlags.Public | BindingFlags.Instance to get public, instance fields.

Upvotes: 4

Related Questions