By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,695 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,695 IT Pros & Developers. It's quick & easy.

reference all properties of a class within the constructor

P: n/a
Is it possible to reference all the public properties that are in a class
within that class's constructor using REFLECTION, and then reference them
with a string variable?
I have a class that will have 35 integer values as public properties that
must be read in from a database. Also, each property of this class will have
the same name as the column name from the dbase table. For that reason, I
would like to just set up a loop to get the property name from the GetName
property in the DataReader, and then assign the value from the read field.
Otherwise I will have to assign each property from the table individually.
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Ric,

Absolutely, here is how you would do it:

// Get all the fields in the class, and map them to the field name.
FieldInfo[] fields = this.GetType().GetFields();

// Cycle through the fields, and add them to a hashtable.
Hashtable map = new Hashttable();

// Cycle.
foreach (FieldInfo fieldInfo in fields)
{
// Add to the hashtable.
map.Add(fieldInfo.Name, fieldInfo);
}

// Make the database call. As you cycle through the fields, you would get
the field info, and set the value.
((FieldInfo) map["column"]).SetValue(this, value);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ric#k" <Ri***@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
Is it possible to reference all the public properties that are in a class
within that class's constructor using REFLECTION, and then reference them
with a string variable?
I have a class that will have 35 integer values as public properties that
must be read in from a database. Also, each property of this class will
have
the same name as the column name from the dbase table. For that reason, I
would like to just set up a loop to get the property name from the GetName
property in the DataReader, and then assign the value from the read field.
Otherwise I will have to assign each property from the table individually.

Nov 16 '05 #2

P: n/a
Ric#k <Ri***@discussions.microsoft.com> wrote:
Is it possible to reference all the public properties that are in a class
within that class's constructor using REFLECTION, and then reference them
with a string variable?
I have a class that will have 35 integer values as public properties that
must be read in from a database. Also, each property of this class will have
the same name as the column name from the dbase table. For that reason, I
would like to just set up a loop to get the property name from the GetName
property in the DataReader, and then assign the value from the read field.
Otherwise I will have to assign each property from the table individually.


Well, you can get all the properties using Type.GetProperties, or get
the property for a particular name using Type.GetProperty. You can then
set or get their values. Which part are you having trouble with?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.