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

Accessing a property using a string containing the property name?

P: n/a
Don
Is it possible to access the property of an object using a string containing
the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(GetPropertyValue(oCustomer,propname) ) <-- returns
oCustomer.FirstName

- Don
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi,
Check out System.Type.InvokeMember()

"Don" <un*****@oblivion.com> wrote in message
news:TDy9d.27860$a41.23499@pd7tw2no...
Is it possible to access the property of an object using a string containing
the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(GetPropertyValue(oCustomer,propname) ) <-- returns
oCustomer.FirstName

- Don

Nov 21 '05 #2

P: n/a
Don,
In addition to System.Type.InvokeMember, I find CallByName to be a handy
wrapper for System.Type.InvokeMember.

http://msdn.microsoft.com/library/de...mefunction.asp

Dim value As Object = CallByName(oCustomer, "FirstName",
CallType.Get)

Also I've used TypeDescriptor.GetProperties with much success.

Dim properties As System.ComponentModel.PropertyDescriptorCollection
properties =
System.ComponentModel.TypeDescriptor.GetProperties (oCustomer)
Dim value As Object = properties("FirstName").GetValue(oCustomer)

Hope this helps
Jay
"Don" <un*****@oblivion.com> wrote in message
news:TDy9d.27860$a41.23499@pd7tw2no...
Is it possible to access the property of an object using a string
containing
the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(GetPropertyValue(oCustomer,propname) ) <-- returns
oCustomer.FirstName

- Don

Nov 21 '05 #3

P: n/a
"Don" <un*****@oblivion.com> schrieb:
Is it possible to access the property of an object using a string containing the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(GetPropertyValue(oCustomer,propname) ) <-- returns
oCustomer.FirstName

Calling a method by its name
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=callbyname>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #4

P: n/a
Don
CallByName is, by far, the simplest of the bunch and wholly adequate for my
needs. Thanks to everyone who responded (and so quickly!)

- Don
"Don" <un*****@oblivion.com> wrote in message
news:TDy9d.27860$a41.23499@pd7tw2no...
Is it possible to access the property of an object using a string containing the name of the property you want to access? Something like:

oCustomer = New Customer
propname = "FirstName"
Console.Write(GetPropertyValue(oCustomer,propname) ) <-- returns
oCustomer.FirstName

- Don

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.