Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using reflection to get the instance name of a variable

Using reflection to get the instance name of a variable

Scheduled Pinned Locked Moved C#
question
8 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    EddieRich
    wrote on last edited by
    #1

    Is there a way to use reflection to get the variable name that a method is called on ? For instance ... class foo { public override string ToString() { return GetType().toString() + " " + GetType().??? VaraibleName ???; } } . . . foo myFoo = new foo(); Console.WriteLine( myFoo.toString()); Should ouput foo myFoo

    A K 2 Replies Last reply
    0
    • E EddieRich

      Is there a way to use reflection to get the variable name that a method is called on ? For instance ... class foo { public override string ToString() { return GetType().toString() + " " + GetType().??? VaraibleName ???; } } . . . foo myFoo = new foo(); Console.WriteLine( myFoo.toString()); Should ouput foo myFoo

      A Offline
      A Offline
      Andrew Rissing
      wrote on last edited by
      #2

      I wouldn't expect you could because variable names are stored only in your code, once you've compiled they go away. You could tell things like class name, etc., but not variable name.

      E 1 Reply Last reply
      0
      • A Andrew Rissing

        I wouldn't expect you could because variable names are stored only in your code, once you've compiled they go away. You could tell things like class name, etc., but not variable name.

        E Offline
        E Offline
        EddieRich
        wrote on last edited by
        #3

        That figures. I just wanted to do some screen dumps while I develop/run and see the values of the variables. I was hoping the variable names could be auto-generated. Anyway, thank you for the quick reply.

        N 1 Reply Last reply
        0
        • E EddieRich

          That figures. I just wanted to do some screen dumps while I develop/run and see the values of the variables. I was hoping the variable names could be auto-generated. Anyway, thank you for the quick reply.

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          RascalRobot wrote:

          wanted to do some screen dumps while I develop/run and see the values of the variables

          It's called the debugger, it comes with VS:doh:


          only two letters away from being an asset

          E 1 Reply Last reply
          0
          • N Not Active

            RascalRobot wrote:

            wanted to do some screen dumps while I develop/run and see the values of the variables

            It's called the debugger, it comes with VS:doh:


            only two letters away from being an asset

            E Offline
            E Offline
            EddieRich
            wrote on last edited by
            #5

            That would work if I were allowed to install VS on the target machine. I am only allowed to install the final .exe on the target which is not connected to our network. In other words, no debug tools allowed. :sigh:

            N P 2 Replies Last reply
            0
            • E EddieRich

              That would work if I were allowed to install VS on the target machine. I am only allowed to install the final .exe on the target which is not connected to our network. In other words, no debug tools allowed. :sigh:

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              There is the thing about being specific in your request. You said, "while I **develop/**run". To me this implied a development environment. Your left with trace output, event logs, remote debugging, etc.


              only two letters away from being an asset

              1 Reply Last reply
              0
              • E EddieRich

                That would work if I were allowed to install VS on the target machine. I am only allowed to install the final .exe on the target which is not connected to our network. In other words, no debug tools allowed. :sigh:

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                Two thoughts - one you could either use Tracing or you could do the following: http://blogs.msdn.com/yosit/archive/2003/06/20/9024.aspx[^]

                the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
                Deja View - the feeling that you've seen this post before.

                1 Reply Last reply
                0
                • E EddieRich

                  Is there a way to use reflection to get the variable name that a method is called on ? For instance ... class foo { public override string ToString() { return GetType().toString() + " " + GetType().??? VaraibleName ???; } } . . . foo myFoo = new foo(); Console.WriteLine( myFoo.toString()); Should ouput foo myFoo

                  K Offline
                  K Offline
                  karam chandrabose
                  wrote on last edited by
                  #8

                  possible if and only if it is a class level variable! it becomes a fields, if it just has a scope in a method alone, 100 % not possible experiencing the experience

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups