471,325 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Get Property Value by Name - is it even possible

Hello,
Object instance is loaded into process with data in its Fields. There is a
db table, that dictates what properties values are needed for a stored
procedure call.
Is it possible to get value of a propery by its name?

so instead of this:

switch (propertyName)
{
case "AA" :
x = myObj.AA;
break;
//and so on
}

I want to do something like this:
x = myObj.PropertyValueGetter(propertyName);

Jan 30 '06 #1
5 18938
Look at the Reflection namespace.

Jan 30 '06 #2
other than that what else is there, or reflections is the only option?

"Bruce Wood" wrote:
Look at the Reflection namespace.

Jan 30 '06 #3
Reflection is about it... why do you ask? Reflection is easy to use
once you get over the (small) learning curve. It does almost exactly
what you wrote:

PropertyInfo pi = myObj.GetType().GetProperty("AA");
x = pi.GetValue(myObj, null);

Jan 30 '06 #4
I was just asking if there was another "correct" way of doing it.
using reflections is not a problem for me at all.
Thank you

"Bruce Wood" wrote:
Reflection is about it... why do you ask? Reflection is easy to use
once you get over the (small) learning curve. It does almost exactly
what you wrote:

PropertyInfo pi = myObj.GetType().GetProperty("AA");
x = pi.GetValue(myObj, null);

Jan 31 '06 #5
I think you'll find that reflection IS the correct way of doing it.

I would use attributes on desired properties.
For example, an 3 objects might represent 3 tables in a database.
Each of those objects has a different property for the PK: (eg.
obj1.userID obj2.UserAccessLevelID obj3.SalaryRatingID)
With an attribute called "PK" in front, you could get the value of the
primary key of any of those 3 objects without knowing the name of the
property even.

Of course you may no even be heading in this direction with your
design, so I'll just shut my big mouth.

Jan 31 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Perttu Pulkkinen | last post: by
2 posts views Thread by endus | last post: by
62 posts views Thread by djake | last post: by
5 posts views Thread by Eric A. Johnson | last post: by
1 post views Thread by John Kraft | last post: by
30 posts views Thread by josh | last post: by
4 posts views Thread by | last post: by

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.