E
Eric Goforth
Hello,
I have a generic subroutine that I pass an object and
fieldname as arguments. The subroutine then uses
reflection to search for the value of the fieldname.
For example:
'Calling the Sub
GetObjectValue(objMyCustomer, "Address.Zip")
'The definition of the Sub
Public Shared GetObjectValue(MyObj as Object, FieldName As
String)
Dim Field As FieldInfo
Dim colObject As Object
If FieldName.IndexOf(".") > 0 Then
Do While strTemp.IndexOf(".") > 0
Field = colObject.GetType.GetField(Left(strTemp, _
strTemp.IndexOf(".")))
If Field Is Nothing Then Return Nothing
strTemp = Mid(strTemp, strTemp.IndexOf(".") + 2, _
Len(strTemp) - strTemp.IndexOf("."))
colObject = Field.GetValue(colObject)
Loop
'A bunch of other junk goes here
End Sub
The logic works, there's a problem when what you're trying
to look for is in a collection. For instance, if I have
collection of Addresses, like home address, mailing
address, work address, etc.
I want to get the Zip code of, say, the work address.
When I call it, GetType.GetField successfully assigns
Field as a {System.Reflection.RuntimeFieldInfo}. I don't
see a way to test for Field (actually Address) being a
collection. Try to do a Ctype and then test for not
returning that particular error code?
I'd then like loop through my collection and return the
zip code for a particular element, like Address(1).Zip.
The reason I'm going to all this trouble is that I want
the piece that calls GetObjectValue not to have to know
what the type of object that it's working with. I just
give him an object and he parses through it looking for
fields in an XML document that is passed to him as well.
-Eric
I have a generic subroutine that I pass an object and
fieldname as arguments. The subroutine then uses
reflection to search for the value of the fieldname.
For example:
'Calling the Sub
GetObjectValue(objMyCustomer, "Address.Zip")
'The definition of the Sub
Public Shared GetObjectValue(MyObj as Object, FieldName As
String)
Dim Field As FieldInfo
Dim colObject As Object
If FieldName.IndexOf(".") > 0 Then
Do While strTemp.IndexOf(".") > 0
Field = colObject.GetType.GetField(Left(strTemp, _
strTemp.IndexOf(".")))
If Field Is Nothing Then Return Nothing
strTemp = Mid(strTemp, strTemp.IndexOf(".") + 2, _
Len(strTemp) - strTemp.IndexOf("."))
colObject = Field.GetValue(colObject)
Loop
'A bunch of other junk goes here
End Sub
The logic works, there's a problem when what you're trying
to look for is in a collection. For instance, if I have
collection of Addresses, like home address, mailing
address, work address, etc.
I want to get the Zip code of, say, the work address.
When I call it, GetType.GetField successfully assigns
Field as a {System.Reflection.RuntimeFieldInfo}. I don't
see a way to test for Field (actually Address) being a
collection. Try to do a Ctype and then test for not
returning that particular error code?
I'd then like loop through my collection and return the
zip code for a particular element, like Address(1).Zip.
The reason I'm going to all this trouble is that I want
the piece that calls GetObjectValue not to have to know
what the type of object that it's working with. I just
give him an object and he parses through it looking for
fields in an XML document that is passed to him as well.
-Eric