473,508 Members | 2,240 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using reflection to access and modify field values

Hi,
Suppose I have a class:
public class Test
{
public string a;
public int b;
}
Now I want to access the value of variable a of the instance of the
class Test (@_@).
public class App
{
public static void Main(string[] args)
{
Test test = new Test();
test.a = "hello";
test.b = 3;

printValue(t, "a"); //print out the value of field a
}

public void printValue(object obj, string fieldName)
{
// print out the value of the given field name
}
}
My question is what to do in the method printValue(object, string)? Thx!

Jun 28 '06 #1
1 1099
<sa****@gmail.com> a écrit dans le message de news:
11**********************@y41g2000cwy.googlegroups. com...

| Suppose I have a class:
| public class Test
| {
| public string a;
| public int b;
| }
| Now I want to access the value of variable a of the instance of the
| class Test (@_@).
| public class App
| {
| public static void Main(string[] args)
| {
| Test test = new Test();
| test.a = "hello";
| test.b = 3;
|
| printValue(t, "a"); //print out the value of field a
| }
|
| public void printValue(object obj, string fieldName)
| {
| // print out the value of the given field name
| }
| }
| My question is what to do in the method printValue(object, string)? Thx!

public void printValue(object obj, string fieldName)
{

FieldInfo fi = obj.GetType().GetField(fieldName);

string result = (string) fi.GetValue(obj);

Console.WriteLine(result);
}

But you do realise that reflection is slower than accessing properties and
fields directly ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 28 '06 #2

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

Similar topics

2
320
by: ichor | last post by:
hi i have read a few examples that use the System.Reflection api , but i cant understand in what way is it helpful. i mean its practical use in the real world thanx
1
1407
by: Roundy | last post by:
We have an application that communicates with a PLC in the field. All of the values from the PLC are written to registry keys under hkey_current_user\software\vb and vba programming\application...
0
1676
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
864
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
1
24184
by: Mudassar | last post by:
i want to get the property value using reflection. Scenerio: i have a status bar on MDI form. it has property named "Panels" and i want to get a specific panel from that panels collection using...
3
4190
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
3
4069
by: Steve Amey | last post by:
Hi all I am using reflection to read the values of properties from a class. The class is returned from a Web Service so I have to access the class using FieldInfo (Using VS 2003 which converts...
6
13137
by: titan nyquist | last post by:
Can I step through a struct using foreach? I want a method that steps through a struct, and prints out the name/value of each member. Can it be done? The error I get when trying this is: ...
6
2921
by: chandramohanp | last post by:
Hi I am trying to modify class instance members using reflection. I am having problem when trying to add/remove/display elements related to List<int> member. Following is the code. class...
0
7231
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
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
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7063
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
7504
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
5640
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,...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.