473,378 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Using reflection on collections w/in objects

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
Nov 20 '05 #1
5 5366
hello,

dim myCollection as object
dim myItem as object
dim myMethodInfo as MethodInfo

myMethodInfo = myCollection.gettype.GetMethod("Item")

if not myMethodInfo is nothing then

myItem = myMethodInfo.Invoke(myCollection ,new object() {MyIndex})
.......

Pascal

"Eric Goforth" <ew*******@yahoo.com> wrote in message news:<0a****************************@phx.gbl>...
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

Nov 20 '05 #2
Public Shared Function GetObjectProperty(ByVal CurrObject As Object,
ByVal FieldName As String) As Object

Dim memInfo As MemberInfo()
Dim colInfo As MemberInfo()
Dim Field As FieldInfo
Dim colField As FieldInfo
Dim colProp As PropertyInfo
Dim colObject As Object
Dim strTemp As String = ""
Dim myObj As Object
Dim myMethodInfo As MethodInfo
Dim myItem As Object

strTemp = FieldName
colObject = CurrObject

'Test to see if FieldName is for a sub
'object ie. Customer.Name
If FieldName.IndexOf(".") > 0 Then
Do While strTemp.IndexOf(".") > 0
If strTemp.IndexOf("(") < strTemp.IndexOf(".") Then
'Object is a collection
'e.g. "Products(0).Name"
Field = colObject.GetType.GetField
(strTemp.Substring(0, strTemp.IndexOf("(")))
colObject = Field.GetValue(colObject)

'At this point colObject is my Collection Object

'If I do the following, myMethodInfo is Nothing
myMethodInfo = colObject.gettype.GetMethod("Item")

'Same happens here
myMethodInfo = colObject.gettype.GetMethod("Item(0)")

'At this point colObject.GetType.GetMember("Item(0)") will
return
'System.Reflection.MemberInfo(), but it only appears to
contain information
'about the structure of the data, not the data itself

'I can see the colObject.Item(0).Name, etc in the watch pane,
but am not allowed
'to set colObject, option strict prohibits late binding
'colObject = colObject.Item(0)
Else
'Object is a NOT a collection
'...
End If
Loop

hello,

dim myCollection as object
dim myItem as object
dim myMethodInfo as MethodInfo

myMethodInfo = myCollection.gettype.GetMethod("Item")

if not myMethodInfo is nothing then

myItem = myMethodInfo.Invoke(myCollection ,new object() {MyIndex})
.......

Pascal

"Eric Goforth" <ew*******@yahoo.com> wrote in message
news:<0a****************************@phx.gbl>...
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



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3
This is kind of a f***ed up way to go about this. You may want to use more
of the reflection namespace to your advantage and see if an object inherits
"CollectionBase" or if it interfaces something like ICollection, IList, or
IEnumerable

Screw string parsing.. =)

CJ

"Eric Goforth" <eg******@bellsouth.net> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...
Public Shared Function GetObjectProperty(ByVal CurrObject As Object,
ByVal FieldName As String) As Object

Dim memInfo As MemberInfo()
Dim colInfo As MemberInfo()
Dim Field As FieldInfo
Dim colField As FieldInfo
Dim colProp As PropertyInfo
Dim colObject As Object
Dim strTemp As String = ""
Dim myObj As Object
Dim myMethodInfo As MethodInfo
Dim myItem As Object

strTemp = FieldName
colObject = CurrObject

'Test to see if FieldName is for a sub
'object ie. Customer.Name
If FieldName.IndexOf(".") > 0 Then
Do While strTemp.IndexOf(".") > 0
If strTemp.IndexOf("(") < strTemp.IndexOf(".") Then
'Object is a collection
'e.g. "Products(0).Name"
Field = colObject.GetType.GetField
(strTemp.Substring(0, strTemp.IndexOf("(")))
colObject = Field.GetValue(colObject)

'At this point colObject is my Collection Object

'If I do the following, myMethodInfo is Nothing
myMethodInfo = colObject.gettype.GetMethod("Item")

'Same happens here
myMethodInfo = colObject.gettype.GetMethod("Item(0)")

'At this point colObject.GetType.GetMember("Item(0)") will
return
'System.Reflection.MemberInfo(), but it only appears to
contain information
'about the structure of the data, not the data itself

'I can see the colObject.Item(0).Name, etc in the watch pane,
but am not allowed
'to set colObject, option strict prohibits late binding
'colObject = colObject.Item(0)
Else
'Object is a NOT a collection
'...
End If
Loop

hello,

dim myCollection as object
dim myItem as object
dim myMethodInfo as MethodInfo

myMethodInfo = myCollection.gettype.GetMethod("Item")

if not myMethodInfo is nothing then

myItem = myMethodInfo.Invoke(myCollection ,new object() {MyIndex})
......

Pascal

"Eric Goforth" <ew*******@yahoo.com> wrote in message
news:<0a****************************@phx.gbl>...
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



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #4
Hello,

It is a little clunky, but there is a reason for it. I have VB6 Adobe
interface. I'd have written it in VB.Net, but .NET doesn't support DDE
very well and OLE doesn't work with the free Acrobat Reader.

At any rate this Adobe interface takes an xml string and a file path,
parses the xml and plugs the xml fields into the fields on the Adobe
Acrobat template.

I have a document class that can be either a .pdf file or other document
format. Each document object is associated with an object in my
application. If it's a .pdf file I instantiate it with an object and a
dataset. The dataset contains the mapping from object property name ==>
field name in my Adobe template.

The document looks at the object you've given it and looks up the values
in the object that are associated with the .pdf field names. It then
builds the xml to pass down to the Adobe interface when it's time to
print or view the document.

So the point is that the whole thing is supposed to be table-driven,
which is why I have stuff like Customers(1).Name, so I can store it in
my table. I can add documents and field mappings in my object and have
them print as needed.

-Eric

CJ wrote:

This is kind of a f***ed up way to go about this. You may want to use
more
of the reflection namespace to your advantage and see if an object
inherits
"CollectionBase" or if it interfaces something like ICollection, IList,
or
IEnumerable

Screw string parsing.. =)

CJ

"Eric Goforth" <eg******@bellsouth.net> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...
Public Shared Function GetObjectProperty(ByVal CurrObject As Object,
ByVal FieldName As String) As Object

Dim memInfo As MemberInfo()
Dim colInfo As MemberInfo()
Dim Field As FieldInfo
Dim colField As FieldInfo
Dim colProp As PropertyInfo
Dim colObject As Object
Dim strTemp As String = ""
Dim myObj As Object
Dim myMethodInfo As MethodInfo
Dim myItem As Object

strTemp = FieldName
colObject = CurrObject

'Test to see if FieldName is for a sub
'object ie. Customer.Name
If FieldName.IndexOf(".") > 0 Then
Do While strTemp.IndexOf(".") > 0
If strTemp.IndexOf("(") < strTemp.IndexOf(".") Then
'Object is a collection
'e.g. "Products(0).Name"
Field = colObject.GetType.GetField
(strTemp.Substring(0, strTemp.IndexOf("(")))
colObject = Field.GetValue(colObject)

'At this point colObject is my Collection Object

'If I do the following, myMethodInfo is Nothing
myMethodInfo = colObject.gettype.GetMethod("Item")

'Same happens here
myMethodInfo = colObject.gettype.GetMethod("Item(0)")

'At this point colObject.GetType.GetMember("Item(0)") will
return
'System.Reflection.MemberInfo(), but it only appears to
contain information
'about the structure of the data, not the data itself

'I can see the colObject.Item(0).Name, etc in the watch pane,
but am not allowed
'to set colObject, option strict prohibits late binding
'colObject = colObject.Item(0)
Else
'Object is a NOT a collection
'...
End If
Loop



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #5
Oups, sorry for the wrong GetMethod for a property :-(

I agree with CJ Taylor, check if the object implements ICollection,
IList, IEnumerable interface (FindInterfaces, GetInterface...) and
cast or use InvokeMember

MyItem = MyCollection.GetTYpe.InvokeMember("Item",
BindingsFlags.GetProperty, Nothing,MyCollection, New Object()
{MyIndex})
Pascal
Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
3
by: TT (Tom Tempelaere) | last post by:
Hi there I am making a service project in C#, and I'm in the process of writing the installer. I made an installer class by using the "Add Installer" menu-item in the design window of the service,...
2
by: Robert W. | last post by:
I'm trying to write a utility that will use Reflection to examine any data model I pass it and correctly map out this model into a tree structure. When I say "any" , in fact there will only be 3...
2
by: Marc | last post by:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine> InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by reflection that 'InvoiceLines' is a...
0
by: aegis | last post by:
Hi All, Im developing a web service in C# that Im running into a few problems with. Essentially I am trying to move a custom class (which contains an array of objects) over the WS to the client....
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
1
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
3
by: Tony Johansson | last post by:
Hello! You can set target Module for AttributeUsage. I just wonder what does it mean with module ? //Tony
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.