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

How can you get a reference to a property by name?

I need to get a reference to an objects property by name. I know how to use
the PropertyDescriptor's GetValue and SetValue methods to get and set the
value of an objects property. But my issue is I need to get a reference to
the property itself.

For example I need to do this:

public Person PopulatePerson(Person p)
{
GetReference(p, "FirstName") = "Chris";
return p;
}

The above example is over simplified compared to what I need to do, but the
end result is the same. I wont know what the property is I need to set until
runtime.

Please keep sample to either C# or VB.NET. Thanks

--
Chris Pietschmann
http://pietschsoft.com
Nov 27 '06 #1
3 1143
crpietschmann <cr***********@discussions.microsoft.comwrote:
I need to get a reference to an objects property by name. I know how to use
the PropertyDescriptor's GetValue and SetValue methods to get and set the
value of an objects property. But my issue is I need to get a reference to
the property itself.

For example I need to do this:

public Person PopulatePerson(Person p)
{
GetReference(p, "FirstName") = "Chris";
return p;
}

The above example is over simplified compared to what I need to do, but the
end result is the same. I wont know what the property is I need to set until
runtime.

Please keep sample to either C# or VB.NET. Thanks
Use typeof(Person).GetProperty("FirstName").

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 27 '06 #2
I already know how to do that. I need to get a Reference to the actual
property of the object.

I need to do this:
GetReference(p, "FirstName") = "Chris";
And this:
string strName = GetReference(p, "FirstName");

Any ideas?

--
Chris Pietschmann
http://pietschsoft.com
"Jon Skeet [C# MVP]" wrote:
crpietschmann <cr***********@discussions.microsoft.comwrote:
I need to get a reference to an objects property by name. I know how to use
the PropertyDescriptor's GetValue and SetValue methods to get and set the
value of an objects property. But my issue is I need to get a reference to
the property itself.

For example I need to do this:

public Person PopulatePerson(Person p)
{
GetReference(p, "FirstName") = "Chris";
return p;
}

The above example is over simplified compared to what I need to do, but the
end result is the same. I wont know what the property is I need to set until
runtime.

Please keep sample to either C# or VB.NET. Thanks

Use typeof(Person).GetProperty("FirstName").

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 27 '06 #3
crpietschmann <cr***********@discussions.microsoft.comwrote:
I already know how to do that. I need to get a Reference to the actual
property of the object.
The PropertyInfo returned by GetProperty *is* the property.
I need to do this:
GetReference(p, "FirstName") = "Chris";
And this:
string strName = GetReference(p, "FirstName");

Any ideas?
Once you've got a PropertyInfo, you can fetch and set the value of the
property for a specific instance using the GetValue and SetValue
methods.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 28 '06 #4

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

Similar topics

11
by: Steve Jorgensen | last post by:
I just came up with a really tidy little solution to the VB/VBA circular reference issue. It only works with Access 2000 or newer, but that's about the only down-side. The issue... You need an...
2
by: Stan | last post by:
I need to change the Url property in a web service proxy class in a generic way. The proxy class looks like this public class Sender : System.Web.Services.Protocols.SoapHttpClientProtocol ... ...
2
by: Ric#k | last post by:
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...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
14
by: 97T | last post by:
Well this is still bugging me. I know there are other ways around this, but for a number of reasons I would like to be able to do this one simple thing. I have a form with a number of controls...
10
by: Arpan | last post by:
Consider the following code that adds a table to a DataSet dynamically: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) 'Create an empty DataSet Dim dSet As...
28
by: Stef Mientki | last post by:
hello, I'm trying to build a simple functional simulator for JAL (a Pascal-like language for PICs). My first action is to translate the JAL code into Python code. The reason for this approach is...
9
by: =?Utf-8?B?VG9tbXkgTG9uZw==?= | last post by:
I don't know if the following is what you are looking for, but to me what you described was using a control array. If you were using vb6 that would be easy, the following articles hopefully...
6
by: kbin | last post by:
Hi all, I'm wondering if it is possible to have some sort of soft reference to an object's properties by using a string representing the property name. Let me explain by an example, here is my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.