Reflect field inside method

J

John Bailo

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


}
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
J

John Bailo

Nicholas said:
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.
 
J

John Bailo

Nicholas said:
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.
 
M

Mike Hofer

John said:
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?
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top