macro for __FUNCTION__ style information

Z

Zytan

I realize there are no macros and no such thing as __FUNCTION__ in C#,
and I know I can do this to get the current function name:

System.Reflection.MethodBase.GetCurrentMethod().Name

But, it's rather long. Is there a way to make something like a macro
or shorthand to use this? If I make a function, it defeats the
purpose.

thx

Zytan
 
A

Angel Caban

Zytan said:
I realize there are no macros and no such thing as __FUNCTION__ in C#,
and I know I can do this to get the current function name:

System.Reflection.MethodBase.GetCurrentMethod().Name

But, it's rather long. Is there a way to make something like a macro
or shorthand to use this? If I make a function, it defeats the
purpose.

thx

Zytan

using MB = System.Reflection.MethodBase;

namespace NS
{
public partial class CLASS
{
public void MethodToName()
{
// ...
MB.GetCurrentMethod().Name
// ...
}
}
}

I can only shorten things up to the MethodBase's name, sorry.
 
O

Otis Mukinfus

I realize there are no macros and no such thing as __FUNCTION__ in C#,
and I know I can do this to get the current function name:

System.Reflection.MethodBase.GetCurrentMethod().Name

But, it's rather long. Is there a way to make something like a macro
or shorthand to use this? If I make a function, it defeats the
purpose.

thx

Zytan

Write a VS 2k5 macro that will type it in for you when you double click it?

Good luck with your project,

Otis Mukinfus

http://www.otismukinfus.com
http://www.arltex.com
http://www.tomchilders.com
http://www.n5ge.com
 
Z

Zytan

using MB = System.Reflection.MethodBase;
MB.GetCurrentMethod().Name

I can only shorten things up to the MethodBase's name, sorry.

Ah right, I knew about that, I should have known. Thanks, the is the
best solution I've seen so far.

Zytan
 
Z

Zytan

Write a VS 2k5 macro that will type it in for you when you double click it?

A possibility, yes, thanks. But, I want to be able to type it quickly
and also have it short and simple so it can be read quickly, as well.
Good luck with your project,

Thanks!

Zytan
 
Z

Zytan

maybe you can write a Visual Studio 2005 Code Snippet if you can not find it
inhttp://msdn2.microsoft.com/en-us/vstudio/aa718338.aspx

You mean so that I can access the snippet and have it write all of it
out for me itself?

Again, this makes typing quick, but still it looks ugly since it's so
big.

I'd just like to be able to make macros to ease debugging. So, i can
write things to the output window quickly. I want this for
__FUNCTION__ style info, but also for other things. It looks like C#
doesn't have much for this. I could make a snippet for each one, but
wow, that's just too much when I could be typing only a few characters
for each if I had macros.

I understand why C# doesn't have macros:
http://blogs.msdn.com/csharpfaq/archive/2004/03/09/86979.aspx
and it's a good thing.

But, I wish it had things to help debugging, since with macros gone,
also goes many of the debugging tools I was happy to have with C++.

Zytan
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Zytan said:
Ah right, I knew about that, I should have known. Thanks, the is the
best solution I've seen so far.

I recommend that you do some performance testing if you are
to call it many times.

I would expect it to carry some overhead.

Arne
 
Z

Zytan

I recommend that you do some performance testing if you are
to call it many times.

I would expect it to carry some overhead.

Overhead for "using MB = System.Reflection.MethodBase;"? How is that
any different than making a fully qualified function call? Aren't
they both compiled to the same IL?

Zytan
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Zytan said:
Overhead for "using MB = System.Reflection.MethodBase;"? How is that
any different than making a fully qualified function call? Aren't
they both compiled to the same IL?

No - overhead of calling:

MethodBase.GetCurrentMethod()

Arne
 
Z

Zytan

No - overhead of calling:
MethodBase.GetCurrentMethod()

Oh, ok. So you were just warning me about how slow it is. I wouldn't
call that overhead, since it's not any slower than the original, and
if I need the info, and this is the only palce to get it, it must be
done.

And its just for debugging purposes anyway, so speed isn't a concern.

thanks

Zytan
 
T

Tim Clark

Try this:

class __FUNCTION_ {
public static string _ {
get {
System.Diagnostics.StackFrame sf = new
System.Diagnostics.StackFrame(1);
return sf.GetMethod().Name;
}
}
}

Usage:
string s = __FUNCTION_._;
 
Z

Zytan

Try this:
class __FUNCTION_ {
public static string _ {
get {
System.Diagnostics.StackFrame sf = new
System.Diagnostics.StackFrame(1);
return sf.GetMethod().Name;
}
}
}

Usage:
string s = __FUNCTION_._;

Damn, again the post didn't go through, i'll ty again:

Thanks Tim. I wonder if it can be extended for __FILE__ and __LINE__.

Also, I would love to have something report the fully qualified method
name! Possible?

Zytan
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

So, I could step up the stack frame and find out exactly what called
what to get here?

Yes. Note that line numbers are not always available.
Actually, what I meant by 'fully qualified' is the class hierarchy.

That is something completely different, but you should also
be able to retrieve that via reflection.

Type BaseType property can be used for that.

Arne
 

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

Top