Reflection

Mondeo

Centurion
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
MrPaul kindly helped me a while ago with the following code

Visual Basic:
'Somewhere in your header section:
Imports System.Reflection

'Assume [i]myVehicle[/i] is the instance of Vehicle which you wish to alter and
'that [i]propertyName[/i] and [i]propertyValue[/i] contain the property name and value

Dim vehType As Type
vehType = myVehicle.GetType() 

'Get the type of myVehicle
'Now, invoke a member to set the property

vehType.InvokeMember(propertyName, _                     
BindingFlags.Public Or BindingFlags.Instance Or _  
    BindingFlags.SetProperty Or BindingFlags.FlattenHierarchy, _ 
    Nothing, _                     
    myVehicle, _ 
    New Object() {propertyValue} _                   
)
This worked fine for the previous problem, now want to use it on a different project, but struggling!

Basically I have 5 instances of a custom class as follows
Visual Basic:
veh2yr10non = New MatrixVehicle(matrixID, "10000", "0", "2")
veh2yr20non = New MatrixVehicle(matrixID, "20000", "0", "2")
veh2yr30non = New MatrixVehicle(matrixID, "30000", "0", "2")
veh2yr40non = New MatrixVehicle(matrixID, "40000", "0", "2")
veh2yr50non = New MatrixVehicle(matrixID, "50000", "0", "2")
I want to dynamically check the value of a property within the matrixvehicle classes.

Ideally I need a method that can do this.
Visual Basic:
Private Function GetClassProperty(byval classinstance as string, byval propertyinclass as string) as string

End Function

Dim s as string = GetClassProperty("veh2yr20non","chprice")
Is this possible?

Thanks
 
Last edited by a moderator:
Use a Dictionary or Hashtable

As with your previous problem, setting a property based on the property name is very straightforward. The real problem here is that you wish to access a variable by its name. I suggest the simplest approach to this would be to use a Dictionary (or Hashtable in .Net 1.x) which associates each MatrixVehicle with its name. For example:

Visual Basic:
'Imports declaration
Imports System.Collections.Generic

'At class level
Private _mvecs As New Dictionary(Of String, Of MatrixVehicle)

'When creating MatrixVehicles
_mvecs("veh2yr10non") = New MatrixVehicle(matrixID, "10000", "0", "2")
_mvecs("veh2yr20non") = New MatrixVehicle(matrixID, "20000", "0", "2")
_mvecs("veh2yr30non") = New MatrixVehicle(matrixID, "30000", "0", "2")
_mvecs("veh2yr40non") = New MatrixVehicle(matrixID, "40000", "0", "2")
_mvecs("veh2yr50non") = New MatrixVehicle(matrixID, "50000", "0", "2")


'Finally, GetClassProperty method
Private Function GetClassProperty(ByVal vecInstance As String, ByVal propertyName As Atring) As String

    'Get the vehicle
    Dim vec As MatrixVehicle = _mvecs(vecInstance)

    'Get the vehicle type
    Dim vehType As Type
    vehType = vec.GetType() 

    'Now, invoke a member to get the property
    Dim retVal As Object
    retVal = vehType.InvokeMember(propertyName, _                     
        BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.GetProperty, _ 
        Nothing, _                     
        vec, _ 
        New Object() {}
    )

    'Return as String
    If (retVal IsNot Nothing) Then
        Return retVal.ToString()
    Else
        Return "Null"

End Function

It might take a little tweaking but hopefully you get the idea. If you wanted to be able to use the method to get the property of any type of object (not just MatrixVehicle) then you could use a Hashtable or a more general Dictionary and it should still work.

Good luck :cool:
 
Back
Top