get name of variable as string?

  • Thread starter Thread starter Zytan
  • Start date Start date
Here you are, Zytan. A complicated and long piece of code for obtaining
a simple value however, it fulfills your requirement. It will print
"i=123" or any variable that you may declare within a class.

J.V.Ravichandran, thanks for your amazing attempt, but, as Jon said,
can you show how it works, before I attempt all of that only to find
it doesn't do what I want? Basically, does it allow this:

int x = 5;
WriteInt(x);

And print the string "x = 5" ?

If so, I could make WriteFloat(), WriteBool(), etc., to allow for
others, that can easily be typed for a quick debugging session. One
function need not handle all types, since I can just make multiple
functions. The purpose is quick typing, and the result is an
unambigious string written out that says just what is going on.

I already have something that prints the method that calls the
function:

class MyClass
{
void MyFunc()
{
int i = 5;
WriteValue("i = "+i);
}
}

will print the string "MyClass.MyFunc(): i = 5"

Zytan
 
Zytan said:
J.V.Ravichandran, thanks for your amazing attempt, but, as Jon said,
can you show how it works, before I attempt all of that only to find
it doesn't do what I want? Basically, does it allow this:

int x = 5;
WriteInt(x);

And print the string "x = 5" ?

I do not think "x" is in the binary code (release).

Arne
 
Zytan,

Here is the usage.

I m sure you understand that we are doing this exercise only to print a
value at runtime. I m sure you know that the simpler way to do it is as
you have done with a simple writeline call. But, I enjoyed the exercise
so I am giving you the same through reflection.

MyClass ob=new MyClass();
int i=123;
ob.SomeMethod(i);


using System;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Windows.Forms;
using System.Reflection;
public class MyClass
{
public void SomeMethod(int i)
{
MyClass myobj;
Type t = this.GetType();
myobj = (MyClass)Activator.CreateInstance(t);
MethodInfo[] mi = t.GetMethods();
foreach (MethodInfo m in mi)
{
if (m.Name == "SomeMethod")
{
ParameterInfo[] pi = m.GetParameters();
foreach (ParameterInfo p in pi)
{
Console.WriteLine(p.Name + "=" + i);
}
}
}

}
static void Main()
{
MyClass ob=new MyClass();
int i=123;
ob.SomeMethod(i);
}
}

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Ravichandran J.V. said:
Here is the usage.

I m sure you understand that we are doing this exercise only to print a
value at runtime. I m sure you know that the simpler way to do it is as
you have done with a simple writeline call. But, I enjoyed the exercise
so I am giving you the same through reflection.

However, the version you showed prints the *parameter* name, not the
*argument* name. In other words, it's fine in the case of:

public void SomeMethod(int i)
and
int i=123;
ob.SomeMethod(i);

but it would not give the desired output in the case of:

public void SomeMethod(int x)
and
int i=123;
ob.SomeMethod(i);

(It would print x=123 instead of i=123)

It would also print the same value for all the parameters, rather than
the value corresponding to the parameter it's printing out. If you're
hardcoding the "+ i" part of the Console.WriteLine, you might as well
just do: Console.WriteLine ("i=" + i);
 
<snip>It would also print the same value for all the parameters, rather
than the value corresponding to the parameter it's printing out. </snip>

Yes. But if this is such an issue, it can be sorted out with an 'if'
condition in the for loop.

Hardcoding the +i is not necessary. It, too, can be reflected upon.


with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Ravichandran J.V. said:
<snip>It would also print the same value for all the parameters, rather
than the value corresponding to the parameter it's printing out. </snip>

Yes. But if this is such an issue, it can be sorted out with an 'if'
condition in the for loop.

Yes - but it would still be the parameter name rather than the argument
name, contrary to the original intention.
Hardcoding the +i is not necessary. It, too, can be reflected upon.

No, it can't. You can't get the values of local variables/parameters
using reflection.
 

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

Back
Top