LINQ and Reflection

E

Evan Nielsen

I have a piece code where I am traversing an objects properties to eventually
get to a string property at the end of the tree.

I am storing a string in the database that is formatted as such:
<property name>.<property name>.<property name>.<property name>
The string can be as many properties long as needed.

I am using reflection to loop through each property and return the object
related to that property, then the next returning the next property on the
list. Code Follows:

For i As Integer = 0 To strDestination.Length - 1
pi = cobraType.GetProperty(strDestination(i))
cobraObj = pi.GetValue(cobraObj,
BindingFlags.GetProperty, Nothing, Nothing, Nothing)
cobraType =
cobraType.GetProperty(strDestination(i)).PropertyType
Next

The initial value of cobraObj is the instance of the upper most object in
the tree that I am traversing. The issue with this is that the initial
object (cobraObj) is being populated by LINQ and for some reason when I end
up calling properties that are nested down two or three levels they return
nothing even though those objects exist if i were to just make a call to them
like Me.propertyname.propertyname.propertyname.

I was thinking that this may be caused by the fact that LINQ does deferred
execution and the properties are not being populated.

What should I do to fix this issue?
 
L

Lloyd Sheen

Evan Nielsen said:
I have a piece code where I am traversing an objects properties to
eventually
get to a string property at the end of the tree.

I am storing a string in the database that is formatted as such:
<property name>.<property name>.<property name>.<property name>
The string can be as many properties long as needed.

I am using reflection to loop through each property and return the object
related to that property, then the next returning the next property on the
list. Code Follows:

For i As Integer = 0 To strDestination.Length - 1
pi = cobraType.GetProperty(strDestination(i))
cobraObj = pi.GetValue(cobraObj,
BindingFlags.GetProperty, Nothing, Nothing, Nothing)
cobraType =
cobraType.GetProperty(strDestination(i)).PropertyType
Next

The initial value of cobraObj is the instance of the upper most object in
the tree that I am traversing. The issue with this is that the initial
object (cobraObj) is being populated by LINQ and for some reason when I
end
up calling properties that are nested down two or three levels they return
nothing even though those objects exist if i were to just make a call to
them
like Me.propertyname.propertyname.propertyname.

I was thinking that this may be caused by the fact that LINQ does deferred
execution and the properties are not being populated.

What should I do to fix this issue?

Could you provide SQL table definition and LINQ code to get the data.
Without that it is dificult to say what may be wrong.

LS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top