itemize variable via reflection

Discussion in 'Microsoft C# .NET' started by Bob, Sep 29, 2007.

  1. Bob

    Bob Guest

    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
     
    Bob, Sep 29, 2007
    #1
    1. Advertisements

  2. Bob <> 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 - <>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Sep 29, 2007
    #2
    1. Advertisements

  3. "Jon Skeet [C# MVP]" <> wrote in message
    news:...
    > Bob <> 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 - <>
    > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    > If replying to the group, please do not mail me too
     
    Ben Voigt [C++ MVP], Oct 1, 2007
    #3
  4. Ben Voigt [C++ MVP] <> 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 - <>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet [C# MVP], Oct 1, 2007
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page