473,399 Members | 4,254 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,399 software developers and data experts.

Identifying class properties at runtime?

At runtime, how do you take a user-supplied string, and then match that up
with a class property name?

For example, I have a User class with the property User.Name. At runtime, I
want the user to type 'set user.name=adam' and the code checks the user
class instance for the property name provided (Name) and assigns that value?

I cannot figure out how to do this without hardcoding the "set" code to know
the Name property in advance. As you can see, with every addition to the
User class, it would require changing the "set" code accordingly. I am
trying to avoid that by writing code that can figure out what properties are
available on a given class at runtime and interacting with them based on
user input.

Any tips? Thanks!
Nov 17 '05 #1
3 1353
You could try:

string propertyName = "name";
object porpertyValue = ...; // The value you want to assign
Type t = user.GetType();
t.InvokeMember
( name
, BindlingFlags.DeclaredOnly
| BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Instance
| BindingFlags.SetProperty
, null
, user
, new object[] { propertyValue }
);

Watch out for exception though.

Nov 17 '05 #2
Thanks Mohammad,

Does this code work on an existing class instance, or does it create a new
instance?

thx
"Mohammad" <m@abdulfatah.net> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
You could try:

string propertyName = "name";
object porpertyValue = ...; // The value you want to assign
Type t = user.GetType();
t.InvokeMember
( name
, BindlingFlags.DeclaredOnly
| BindingFlags.Public
| BindingFlags.NonPublic
| BindingFlags.Instance
| BindingFlags.SetProperty
, null
, user
, new object[] { propertyValue }
);

Watch out for exception though.

Nov 17 '05 #3
Your welcome :)

It works on an existing class instance. The fourth argument named
"user", should be an existing class instance. So in your code, you'll
have to parse the string, get the name of the class instance, and use
that as the fourth argument to begin invoke. You would also extract the
property name, in your case "name", and also pass it as the first
argument. Finaly, extract the value, in your case "adam", and pass it
in an array of objects as the final argument.

Nov 17 '05 #4

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

Similar topics

5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
4
by: Paul Wu | last post by:
Is there a way to constract a derived class that hides certain public members of a base class ? With the following code, a class that derives from DerivedClass can still see the member "Name" in the...
3
by: Buddy Ackerman | last post by:
In .NET 1.x the validating reader used an XmlTextReader which had LineNumber and LinePosition properties so you could report where the validation took place. In 2.0 the XmlReader is a validating...
3
by: User1013 | last post by:
I have the Javascript definitive guide book but I'm hoping someone can tell me what "language concepts" are being employed by the following code, so that I can look up the right parts in the book:...
15
by: Anthony Greene | last post by:
This is probably a very introductory object-oriented question, but it has been nagging me for years, and since I've never been able to find the right answer, I've had to work around it with...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
5
by: Tom P. | last post by:
I am having the following problem: I create a FileSystemWatcher and wait for events. When the event does happen I refresh a FileSystemInfo list and set properties accordingly (IsFile, IsDir,...
7
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using...
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: 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: 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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.