Reflection Solution?

Reflection Solution?

Old forum URL: forums.lhotka.net/forums/t/1602.aspx


NetStream posted on Thursday, October 26, 2006

Let's say that I have an editable root object with 500 items and I want to update a half dozen child objects with a varying number of items all having a linked field to the root object.  I need to retrieve a value from the root object based on the linked field and store it in the child object.  I want to be able to traverse the child objects one property at a time searching for the same property in the root object and storing the value of the root property in the child object at runtime.  How might I accomplish this?

boo replied on Thursday, October 26, 2006

Let's see if I understand:

You have a list (X) with 500 items (Y).

You want to update a few that 'have a linked field to the root object'.   By this do mean this.Parent.SomeProperty (where 'this' is the child object)?

You need to 'retrieve a value from the root object based on the linked filed and store it in the child object'.   Can't you just do this.Parent.SomeProperty ('this' being of type Y)?

And the last part I don't understand what you're trying to do...if you have a property in the X object called "Name" and a property in the Y object called "Name" can't  you just do this.Name = Parent.Name ('this' being of type Y)?

Perhaps I can be of more assistance if you phrase the question differently.

NetStream replied on Thursday, October 26, 2006

The problem is that you do not know what the properties on either side are beforehand.  You have to parse through the properties in the smaller tables looking for those properties in the larger table.  You do not know what the properties are until runtime.  Otherwise, you could do a this.name=parent.name.  That is what makes this challenging.  I can use reflection to locate the property in the larger table but I have not been able to find any way to change the value in the located field because reflection is using an empty instantiated class not the actual class I am using in the code.

Bayu replied on Friday, October 27, 2006

Hey,

I don't understand what challenge you actually face, your description is a bit fuzzy. Tongue Tied [:S]

However, I do know reflection, so perhaps I can answer your question anyways. Wink [;)]

- TypeDescriptor.GetProperties(oObject.GetType) will return a PropertyDescriptorCollection for a particular type of object
- each PropertyDescriptor in this PropertyDescriptorCollection is indeed not tied to a particular instance of the object, it's just metadata
- however, with a PropertyDescriptor at hand, you can obtain a PropertyInfo object using oInfo = oObject.GetType.GetProperty(somePropertyDescriptor.Name)
- then, using oValue = oInfo.GetValue(oObject, Nothing) you get the particular value of the particular instance


- additionally, if you want to use type conversions then better make use of the mechanisms that also drive the PropertyGrid control, i.e.: TypeConverter
- you get a typeconverter using oConverter = somePropertyDescriptor.Converter
- to check if it can convert property values to a particular target type, check oConverter.CanConvertTo(GetType(String)) (replace String with anything you need)
- if this returns true, you can perform the actual conversion with: sValue = DirectCast(oConverter.ConvertTo(oValue, GetType(String)), String)


This is pretty powerful stuff, I guess  you should be able to solve your challenge using these techniques.

Bayu

NetStream replied on Friday, October 27, 2006

Your discussion was right on!  I really appreciate your assistance.  I will certainly pay this forward!

 

Big Smile [:D]

Bayu replied on Saturday, October 28, 2006

Glad to be of help.
Your gratitude is much appreciated in turn as well. Smile [:)]

Bayu

Copyright (c) Marimer LLC