Access class properties in code (property names in datatable)

Mondeo

Well-known member
Joined
Nov 10, 2006
Messages
128
Location
Sunny Lancashire
I have a class called vehicledataset which has 130 properties, all strings

I have a datatable with two columns, name and value. For example

Name Value
combined_make Ford
combined_model Mondeo ST24

All the items in my name column tie up to the property names in my class. I want to loop through the datatable (i know how to do this using the rows collection) and assign each value to the matching property name in my class.

But, I dont know how to refer to the property name dynamically.

Cheers
Ben
 
It just so happens that I have already written the code for you, and it can be found right here within Xtreme .Net Talk. Look.
 
Reflection 101

Basically, marble_eaters code is just a helper class for working with reflection - the part of the .Net framework which deals with inspecting types, creating object instances and invoking their members. The System.Reflection namespace contains the classes that achieve this.

First of all, you need to get the Type of the object you wish to invoke a member of. In this case, it sounds like your type might be called Vehicle. Then, you call the InvokeMember method of the Type object which... invokes a member - in this case, to set a property. The InvokeMember method takes a number of parameters and can be quite confusing at first glance - hence marble_eaters helper class - but it is all quite straightforward.

Code:
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} _
                    )

As you can see, the call to InvokeMember is quite complex. Here are what the parameters mean:

  1. The first parameter specifies the name of the member - in this case the property we wish to change.
  2. The second parameter specifies the way in which the member should be located - in this case, look for a public, instance (not static) member, and we wish to set a property. The FlattenHierarchy flag specifies that inherited members should be included.
  3. The third parameter allows us to specify a custom Binder object (to find the member to invoke). In most cases, this can be Nothing.
  4. The fourth parameter specifies the instance to invoke the member on - in this case our instance myVehicle.
  5. The fifth parameter specifies the arguments to be passed to the invokation - in this case, the value of the property we wish to set.

Good luck :cool:
 
Thats very helpful, thanks for taking the time to help me and expain it in detail like that. I think it can get it working from that example you did.

And thanks marble_eater, your code makes a lot more sense to me now :)

Ben
 
Back
Top