473,387 Members | 1,834 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,387 software developers and data experts.

itemize variable via reflection

Bob
Hi,
Does anyone know if it is possible to use reflection to determine the
names and value types of the variables currently in scope?

Thanks,
Bob

Sep 29 '07 #1
3 2018
Bob <bs********@yahoo.comwrote:
Does anyone know if it is possible to use reflection to determine the
names and value types of the variables currently in scope?
No - in particular, local variables and parameters won't *have* names
when debug information hasn't been built.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 29 '07 #2

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Bob <bs********@yahoo.comwrote:
>Does anyone know if it is possible to use reflection to determine the
names and value types of the variables currently in scope?

No - in particular, local variables and parameters won't *have* names
when debug information hasn't been built.
You can get the types of local variables for any method though. See the
example in the LocalVariableInfo class. To get the current method you can
use the StackTrace class (is there a more efficient way to get just the top
method?)
>
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 1 '07 #3
Ben Voigt [C++ MVP] <rb*@nospam.nospamwrote:
No - in particular, local variables and parameters won't *have* names
when debug information hasn't been built.

You can get the types of local variables for any method though. See the
example in the LocalVariableInfo class.
Ah, haven't looked at that. I wonder if it gives entries for local
variables which are introduced by the compiler but don't represent
actual local variables in the original source code. Must play some
time.
To get the current method you can
use the StackTrace class (is there a more efficient way to get just the top
method?)
using System;
using System.Reflection;

public class ConvertFile
{
public static void Main(string[] args)
{

MethodBase b = MethodBase.GetCurrentMethod();
Console.WriteLine (b.Name);

MethodBody body = b.GetMethodBody();

foreach (LocalVariableInfo info in body.LocalVariables)
{
Console.WriteLine (info.LocalType);
}
}
}

Intriguing...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 1 '07 #4

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

Similar topics

4
by: Lucas Sain | last post by:
Hi, I think thta for this I have to use reflection... but I'm not shure. How can I find/get an object at runtime by looking for its name that is stored in a variable. For example: I have a...
3
by: Ken Durden | last post by:
Hey, I posted this a few months ago and got completely off-the-chart responses. At the time I wasn't using reflection very much, but more and more I find myself wanting to use it to write...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
2
by: Cary | last post by:
In my project, I want to display the version number on the splash screen and in the help section of my application. I have a variable I created called appVersion. When I go to my deployment project...
2
by: André | last post by:
I try to initialize some Public Variable in a class use for code-behind The idea is to check all uxMsgXXX variables, declare in top of my class and assign the string value from a DB request. Why...
4
by: Mike TI | last post by:
April 22, 2006 Hi all I want to execute a command stored in a variable, eg I may store: Form1.Show() or Form2.Show()
11
by: David Veeneman | last post by:
I need to get a parent's private variable from within a control. Here's what I'm doing: I have a control that I want to be able to detect the presence of a particular (System.ComponentModel)...
45
by: Zytan | last post by:
Shot in the dark, since I know C# doesn't have macros, and thus can't have a stringizer operator, but I know that you can get the name of enums as strings, so maybe you can do the same with an...
4
by: =?Utf-8?B?Y2xhcmE=?= | last post by:
Hi all, what is the difference between the late binding and reflection? clara -- thank you so much for your help
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.