PC Review


Reply
Thread Tools Rate Thread

itemize variable via reflection

 
 
Bob
Guest
Posts: n/a
 
      29th Sep 2007
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

 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      29th Sep 2007
Bob <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
 
Ben Voigt [C++ MVP]
Guest
Posts: n/a
 
      1st Oct 2007

"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Bob <(E-Mail Removed)> wrote:
>> 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 - <(E-Mail Removed)>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      1st Oct 2007
Ben Voigt [C++ MVP] <(E-Mail Removed)> wrote:
> > 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I itemize all of the numbers within multiple sets of range =?Utf-8?B?QnVzeURhZA==?= Microsoft Access VBA Modules 1 23rd Nov 2006 01:09 AM
VBA code to itemize range names and associations? Larry A Microsoft Excel Programming 14 2nd Jul 2005 12:42 PM
How do I set up to itemize bills by the year? =?Utf-8?B?TXJzdGVh?= Microsoft Word Document Management 2 17th Jan 2005 06:36 PM
I am planning a vacation, how do i itemize my expenses? =?Utf-8?B?QW1kaXJldGg=?= Microsoft Excel New Users 3 24th Nov 2004 04:17 AM
itemize recordset column info? Frank B Microsoft Access VBA Modules 2 18th Dec 2003 05:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:13 PM.