I have a VB.NET class called ticket that has several public properties of type 'field'. I would like to have a way that I can iterate through all of these properties (with a for each) and perform a specific task on each of them. I thought that maybe the best way was to create a list(Of field) and fill the list with the 'field' properties of that class. What I don't know how to do is get the properties into the list dynamically so that if I add properties in the future I don't have to type them into the list manually. Any thoughts on how I might do this? I tried searching for and found some examples of using reflection but I could only figure out how to get at the name of the property and not the property itself.
Here is an example of a class:
Public Class ticket
Public Property location As New field
Public Property user As New field
Public Property callType As New field
Public Property dateOfCall As New field
Public Property tech As New field
Public Property description As New field
Public Property myFields As New List(Of field)
'What if field had a property of value and I wanted to increment all of the fields in this class by one
Public Sub plusOne()
For Each x As field In myFields()
x.value += 1
Next
End Sub
End Class
You want to use Reflection, which just means inspecting the types in an assembly. You can do this via the System.Reflection namespace.
See the following article on the msdn magazine for examples of reflection in VB.Net:
http://msdn.microsoft.com/en-us/magazine/cc163750.aspx
An example of iterating over the members of a type in that article is as follows:
Dim t As Type = GetType(AcmeCorp.BusinessLogic.Customer)
For Each member As MemberInfo In t.GetMembers
Console.WriteLine(member.Name)
Next
Again as the previous answer - you would use reflection. To call as an example Add
on a List(Of T)
I would do this.
Public Class Test
Public Property SomeList As List(Of String)
End Class
Then use the following code to call add on a List(Of String)
Dim pi As PropertyInfo = GetType(Test).GetProperty("SomeList")
Dim mi As MethodInfo = GetType(List(Of String)).GetMethod("Add")
Dim t As New Test()
t.SomeList = New List(Of String)
mi.Invoke(pi.GetValue(t, Nothing), New Object() {"Added through reflection"})
As the previous answers have said, you need to use System.Reflection to get the properties of the class. Then check the property is the type that you want.
Hopefully this should give you what you want. If you run the code you will see that it only takes the properties of the specified type. If you want to have all properties, remove the where statement on the for each loop.
Imports System.Reflection
Module Module1
Sub Main()
' Create a list to hold your properties
Dim myList As New List(Of MyProperty)
' check each property for its type using the where statement below. Change integer to "Field" in your case
For Each el In GetType(Test).GetProperties.Where(Function(p) p.PropertyType = GetType(Integer))
' add each matching property to the list
myList.Add(New MyProperty With {.Name = el.Name, .GetMethod = el.GetGetMethod(), .SetMethod = el.GetSetMethod()})
Console.WriteLine(el.Name & " has been added to myList")
Next
Console.Read()
End Sub
Public Class MyProperty
Public Property Name As String
Public Property GetMethod As MethodInfo
Public Property SetMethod As MethodInfo
End Class
Public Class Test
Private var1 As String
Private var2 As String
Private var3 As String
Private var4 As String
Public Property myInt1 As Integer
Public Property myInt2 As Integer
Public Property myInt3 As Integer
Public Property myInt4 As Integer
End Class
End Module
Hope that helps