I can easily list all controls in a Form, after creating an instance of it.
Is there any mechanism to list all declared variables or such objects?
Perhaps I shall call it declarations. Only top-level declarations are enough.
Let's assume we have MyForm
Form with such top-level declarations:
Dim Town as String
Dim ZIP as String
Dim StreetName as String
Dim StreetNo as String
Public dtCountries as DataTable
Public LstCities as List(Of String)
...
Pseudo-code example:
Dim MyForm as New MyForm ' create instance of the form
Dim dtVariables as New Datatable ' create a datatable to store found objects
dtVariables.Columns.Add("ID", GetTy(Int32))
dtVariables.Columns.Add("VariableName", GetTy(String))
dtVariables.Columns.Add("VariableType", GetTy(String))
For Each Varbl In MyForm.***variables*** ' <<< (how) to read all variables
Dim nr as Datarow = dtVariables.NewRow
nr("ID") = dtVariables.Rows.Count + 1
nr("VariableName") = Varbl.Name
nr("VariableType") = Varbl.GetType.ToString.Replace("System.Windows.Forms.", "")
dtVariables.Rows.Add(nr) ' add found object/variable to our datatable
Next
The result I am looking for is something like:
1 Town String
2 ZIP String
3 StreetName String
4 StreetNo Int32
5 dtCountries DataTable
6 LstCities List(Of String)
... ... ...
I know that I can read MyForm.designer.vb
file and look there for declarations.
This question is about getting it from an object model of a Form / instance of a Form.
An example using a filtered collection of FieldInfo objects returned by
Type.GetType().GetFields()
Since you want this method to return both Public and non-Public Fields, the collection must be filtered because, since this is a Form class, it will include all the controls a Form contains.
The collection of
FieldInfo
is then filtered usingFieldType.Namespace
, where theNamespace
is notSystem.Windows.Forms
.The BindingFlags are set to
Instance | Public | NonPublic | DeclaredOnly
.When the Field represents a Collection (List, Dictionary etc.), the Type.GenericTypeArguments property needs to be parsed to extract the arguments Collection.
I'm using a couple of helper functions to clean up the Fields Name and to retrieve the collection of arguments as a formatted string.
Using the sample Fields you posted (I added a Dictionary to test the output):
this is the result:
If Interpolated String is not available (before VB.Net version 14), use a Composite Format string:
can be expressed as: