473,322 Members | 1,431 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,322 software developers and data experts.

Copy: Hashtable to Properties

Hello,

I have a values stored in a Hashtable hash and I want assign them as
properties to an object obj

obj.x1 = (String)hash[x1];
obj.x2 = (long)hash[x2];
obj.x3 = (int)[x3];
.....
Is there a better (more reusable) way, like this Pseudocode:

foreach (DictionaryEntry de in hash) {
obj.(de.key) = (de.Value.GetType())hash[de.Value];
}

Thanks!
Nov 17 '05 #1
3 1313
You'd have to use reflection, something like this:

Type objType;
PropertyInfo property;

objType = obj.GetType();

foreach( DictionaryEntry de in hash ) {
property = objType.GetProperty( de.key );
property.SetValue( obj, hash[ de.key ] );
}

You may have to cast the value out of the Hashtable to match what the
property expects, so you may need to have a switch statement in there
that looks at property.PropertyType.

HTH
Andy

Nov 17 '05 #2
"Andy" <aj********@capcitypress.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
property.SetValue( obj, hash[ de.key ] ); You may have to cast the value out of the Hashtable to match what the
property expects, so you may need to have a switch statement in there
that looks at property.PropertyType.


No casting is required - SetValue would just cast it back to object anyway.

---
Adam Clauss
Nov 17 '05 #3
Good point.

Nov 17 '05 #4

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

Similar topics

2
by: Wamiq Ansari | last post by:
Hi, I am using a System.Windows.Forms.Label object as key in my Hashtable. One thing that has surprised me is that if I create two Label objects with same properties (ie. Name,...
1
by: francois | last post by:
Hi, I have a webservice that I am using and I would like it to return an XML serialized version of an object. the class of the object is defined serializable as the following: public...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
0
by: mnmnm1951 | last post by:
I am working with Sharepoint Services and am trying to add the fileversion Metadata to the Versions.aspx page using the example from the SDK. I have not done much with hashtables but it all looks...
6
by: harvie wang | last post by:
Hi, I want to implement a copy construct class, but the class have as Hashtable,how to do? public class A { private Hashtable _data; public A() { _data = new Hashtable(); }
5
by: Dave | last post by:
Hi all, Is it possible to populate a listbox from a hashtable (complexbind) where the ValueMember of the listbox maps to the "key" member of the hashtable AND the DisplayMember of the listbox maps...
2
by: Ray Cassick \(Home\) | last post by:
I have a hashtable in a class that contains key (strings) value (integers, strings, longs, etc...) data pairs. I want to move the data to another (different) class instance for processing. ...
5
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's...
12
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi all I'm creating a hashtable of a list of users in AD. The keys are UserID and Password. I want to loop through the table to compare the userID and password with values that a user has...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.