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
} 5 2050
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
}
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.
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.
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?
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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,...
|
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...
|
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...
|
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.
...
| |
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...
|
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 =...
|
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...
|
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...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |