Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
It just so happens that I have already written the code for you' date=' and it can be found right here within Xtreme .Net Talk. Look.

 

Thanks for that, but its way over my head. I dont understand any C# unfortunately :(

Posted

Reflection 101

 

Basically, marble_eater's 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_eater's helper class - but it is all quite straightforward.

 

'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:

Never trouble another for what you can do for yourself.
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...