Reflection question
-
I try to use reflection to get a list of member fields in a form class.
Public Class BaseForm
Inherits System.Windows.Forms.Form
......
Friend WithEvents MnuBaseFile As System.Windows.Forms.MenuItem
Friend WithEvents MnuBaseWindow As System.Windows.Forms.MenuItem
Friend WithEvents MnuBaseFileExit As System.Windows.Forms.MenuItem
Friend WithEvents BaseFormTimer As System.Timers.Timer
Friend WithEvents MenuMainSeparator1 As System.Windows.Forms.MenuItem
Friend WithEvents HelpProvider As System.Windows.Forms.HelpProvider.....
Dim fi As System.Reflection.FieldInfo() = My.Forms.BaseForm.GetType().GetFields()But I failed, the length of fi is zero, the fi is empty array.
-
I try to use reflection to get a list of member fields in a form class.
Public Class BaseForm
Inherits System.Windows.Forms.Form
......
Friend WithEvents MnuBaseFile As System.Windows.Forms.MenuItem
Friend WithEvents MnuBaseWindow As System.Windows.Forms.MenuItem
Friend WithEvents MnuBaseFileExit As System.Windows.Forms.MenuItem
Friend WithEvents BaseFormTimer As System.Timers.Timer
Friend WithEvents MenuMainSeparator1 As System.Windows.Forms.MenuItem
Friend WithEvents HelpProvider As System.Windows.Forms.HelpProvider.....
Dim fi As System.Reflection.FieldInfo() = My.Forms.BaseForm.GetType().GetFields()But I failed, the length of fi is zero, the fi is empty array.
The GetFields method with no parameters returns only public fields. Try using the other overload and specifying what you want to search for by defining the binding flags. For example:
Dim fi As System.Reflection.FieldInfo() = My.Forms.BaseForm.GetType().GetFields(BindingFlags.NonPublic Or BindingFlags.Instance)
For more information, see: Type.GetFields Method (BindingFlags)[^] and BindingFlags Enumeration[^]
-
The GetFields method with no parameters returns only public fields. Try using the other overload and specifying what you want to search for by defining the binding flags. For example:
Dim fi As System.Reflection.FieldInfo() = My.Forms.BaseForm.GetType().GetFields(BindingFlags.NonPublic Or BindingFlags.Instance)
For more information, see: Type.GetFields Method (BindingFlags)[^] and BindingFlags Enumeration[^]