0

I'm going to pass the current class name as a parameter to some of the functions inside my codes.

I have many calls like this inside my code:

return Ok(new WebServiceResult(ErrorCodes.ERROR_CODE_MINUS_002_INVALID_REQUEST_DATE,
          MethodBase.GetCurrentMethod().DeclaringType.Name));

The part MethodBase.GetCurrentMethod().DeclaringType.Name is intended to return name of the current class.

How can I shorten this MethodBase.GetCurrentMethod().DeclaringType.Name using C# syntaxes?

I was looking for a thing like macros in C/C++, something such as:

#define CLASSNAME MethodBase.GetCurrentMethod().DeclaringType.Name

but it seems Macros are not supported in C# and it is not a good practice in C# if I understood correctly. However, for making my code clearer and more readable I'm looking for something to replace this snippet and make it shorter.

P.S. I don't want to fetch name of calling class inside the called method itself but I'm looking for a way to shorten my code and make it more clear.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
VSB
  • 9,825
  • 16
  • 72
  • 145
  • CallerMemberAttribute has nothing to fetch calling ClassName. It got the name of Calling Method Name but not for the Class Name itself. – VSB Mar 09 '22 at 16:36

2 Answers2

1

Define a class constant by using nameof. The compiler checks that this name exists. Rename refactorings will also change this name inside nameof.

public class MyClass
{
    private const string ClassName = nameof(MyClass);
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

If this is for logging I would consider using attributes to insert the file-name. If you use one file per class, and you probably should, it provide a similar way to discover the source of the log-message. Using these attributes will make the compiler insert the parameters at compile time, making it way faster than using reflection.

Example

public void TraceMessage(string message,
        [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
        [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
        [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
{
    System.Diagnostics.Trace.WriteLine("message: " + message);
    System.Diagnostics.Trace.WriteLine("member name: " + memberName);
    System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
    System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);
}

If must have the actual class name this approach will not be feasable. A possible workaround could be to use a separate static method that inspects the call stack to find the calling method, allowing you to provide a shorter method. Inspecting the call stack will be somewhat slow, but everything involving reflection is somewhat slow. Just remember to mark your helper method as non-inlinable: [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]

JonasH
  • 28,608
  • 2
  • 10
  • 23