473,403 Members | 2,359 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,403 software developers and data experts.

Reflect field inside method


Is it possible to Reflect a variable inside a method?

I can Reflect a class field using GetField, but can I reflect a
variable/field from inside member?

For example, I want to Reflect samp inside of the method:

public int myMethod()
{

string samp = "five";

// Get type information for the method.
// This produces a result:

MethodInfo mInfo = this.GetType().GetMethod("myMethod");

//I though this would give me access to samp
//but it returns null:

FieldInfo fi = this.GetType().GetField("samp");

string varValue = (string) fi.GetValue( this );
Debug.WriteLine(varValue); // no value written
}
Nov 22 '05 #1
5 2048
Jon,

In .NET 2.0, you can get the MethodBody for the method using the
GetMethodBody instance on the MethodInfo class. Once you do that, you can
access the LocalVariables property which will return an
IList<LocalVariableInfo>, which you can iterate through to find your
parameter. After that, all you can get is the name of the variable, and the
type.

You can not get the value though. To get the value, you would have to
hook into the CLR with unmanaged code (through the debugging interfaces, no
doubt) and get the value from there (not trivial, to say the least).

What are you trying to do?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Bailo" <ja*****@texeme.com> wrote in message
news:XL********************@speakeasy.net...

Is it possible to Reflect a variable inside a method?

I can Reflect a class field using GetField, but can I reflect a
variable/field from inside member?

For example, I want to Reflect samp inside of the method:

public int myMethod()
{

string samp = "five";

// Get type information for the method.
// This produces a result:

MethodInfo mInfo = this.GetType().GetMethod("myMethod");

//I though this would give me access to samp
//but it returns null:

FieldInfo fi = this.GetType().GetField("samp");

string varValue = (string) fi.GetValue( this );
Debug.WriteLine(varValue); // no value written
}

Nov 22 '05 #2
Nicholas Paldino [.NET/C# MVP] wrote:
Jon,

In .NET 2.0, you can get the MethodBody for the method using the
Nick,

Thanks for your help.

Unfortunately I'm bound to 1.1
GetMethodBody instance on the MethodInfo class. Once you do that, you can
access the LocalVariables property which will return an
IList<LocalVariableInfo>, which you can iterate through to find your
parameter. After that, all you can get is the name of the variable, and the
type.

You can not get the value though. To get the value, you would have to
hook into the CLR with unmanaged code (through the debugging interfaces, no
doubt) and get the value from there (not trivial, to say the least).
Wow!

That's amazing, since if I set up a class member, and set the value of
that, it's trivial to get that value using Reflections. I guess that's
because it's known at runtime...
What are you trying to do?


I want to set up an XML file that relates some web method parameters to
variables inside my code. Then, I will read those pairs and put them
in a database (the database is read by a dynamic proxy generator that
calls the web service).

But, I need to take the string variable name, and reflect it with a
FieldInfo.GetValue to get the value.
Nov 22 '05 #3
Nicholas Paldino [.NET/C# MVP] wrote:
You can not get the value though.


I think what I might do is create a struct, defined in my class, then
set the fields of my struct along the way.

Then, when I need to retrieve the value, I'll retrieve it from there.
It should be the same process as getting the field information for a any
public member of the class.
Nov 22 '05 #4

John Bailo wrote:
Nicholas Paldino [.NET/C# MVP] wrote:
You can not get the value though.


I think what I might do is create a struct, defined in my class, then
set the fields of my struct along the way.

Then, when I need to retrieve the value, I'll retrieve it from there.
It should be the same process as getting the field information for a any
public member of the class.


Will that work with a struct? Structs follow VALUE semantics, so the
method won't be able to change the values on the struct. Or am I not
understanding value semantics?

Nov 22 '05 #5
It depends on when the assignment took place. It doesn't matter that he
is using a structure to store the information, but if the type of
information that is being stored (the value that is being stored) is a
structure, then that structure will be copied over to the structure storing
it, and if there are changes between the time of assignment and the time
that the values are written, you can't pick up on that.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike Hofer" <kc********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

John Bailo wrote:
Nicholas Paldino [.NET/C# MVP] wrote:
> You can not get the value though.


I think what I might do is create a struct, defined in my class, then
set the fields of my struct along the way.

Then, when I need to retrieve the value, I'll retrieve it from there.
It should be the same process as getting the field information for a any
public member of the class.


Will that work with a struct? Structs follow VALUE semantics, so the
method won't be able to change the values on the struct. Or am I not
understanding value semantics?

Nov 22 '05 #6

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

Similar topics

7
by: bass-man | last post by:
I am trying to include a dynamically changeable file in my website with PHP and Javascript so I am using a hidden field to hold the name of the file I want to include, and I am using the$_GET...
7
by: Mike | last post by:
I've been trying for the past week to put a simple code together. I have done a LOT of searching, found scripts showing the functions I would like to use, however when I mix them it all goes wrong,...
4
by: Cheng Mo | last post by:
I just swtiched my working language from java to C++. In java, there is one mechanism called reflect which enable us to have knowledge of internals of a given class at run-time. That's one basic...
5
by: John Bailo | last post by:
Is it possible to Reflect a variable inside a method? I can Reflect a class field using GetField, but can I reflect a variable/field from inside member? For example, I want to Reflect samp...
2
by: MLH | last post by:
I have a form based on a table. The table has a Yes/No field. Its an A97 table. The form has a button that updates the value in the control from No to Yes for the record currently being edited. ...
4
by: William Stacey | last post by:
Using the following struct def, how can I tell (using reflection) if "ba" has the marshal attribute and get the "ByValArray" and maybe even the size? In the bigger picture, given a struct (or a...
2
by: Norton | last post by:
Dear all, I have created many objects Currently i create a toString function, which is to list all variables value inside this object Somthing like Public Class ABC Dim sName as string =...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
0
by: AliRezaGoogle | last post by:
Dear members, I have a datagrid and a textbox on my form. I bound both of them to a common datasource( an arbitrary datatable). When I change a text inside textbox I expect that value of same...
2
by: Mikhail Teterin | last post by:
Hello! I'm going through the fields of a class one at a time and need to handle differently depending on whether they are arrays or scalars. What's the right way to make the distinction? The...
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.