Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Get Calling Method Without Stack Trace

Tags:

c#

.net

I've noticed that .NET 4.5 has a new attribute called [CallerMemberNameAttribute] which, when attached to a parameter of a method, will supply the string name of the method that called that method (if that makes sense).

However, unfortunately (because I want to make something with XNA) I'm only targeting .NET 4.0.

I want to be able to do something like:

void MethodA() {
   MethodB();
}

void MethodB() {
   string callingMethodName = (...?);
   Console.WriteLine(callingMethodName);
}

Where my output would be MethodA.

I know I could do this via stack trace, but that's a) Unreliable and b) Sloooow... So I'm wondering if there's any other way to glean that information, however that may be...

I was hoping for any ideas or knowledge that anyone might have on the issue. Thanks in advance :)

like image 224
Xenoprimate Avatar asked Jun 30 '12 20:06

Xenoprimate


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming languagegeneral-purpose programming languageIn computer software, a general-purpose programming language (GPL) is a programming language designed to be used for building software in a wide variety of application domains, across a multitude of hardware configurations and operating systems.https://en.wikipedia.org › wiki › General-purpose_programmi...General-purpose programming language - Wikipedia created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


1 Answers

If you use Visual Studio 2012 to compile this, you can write your own CallerMemberNameAttribute and use it the same way you would with .NET 4.5 even if you still target .NET 4.0 or 3.5. The compiler will still perform the substitution at compile time, even targeting an older framework version.

Just adding the following to your project will do the trick:

namespace System.Runtime.CompilerServices
{
    public sealed class CallerMemberNameAttribute : Attribute { }
}
like image 128
Reed Copsey Avatar answered Oct 01 '22 13:10

Reed Copsey