Home > Back-end >  C#: How to shorten MethodBase.GetCurrentMethod().DeclaringType.Name inside my code?
C#: How to shorten MethodBase.GetCurrentMethod().DeclaringType.Name inside my code?

Time:03-10

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.

CodePudding user response:

The closest concept might be the using directive. It may shorten the call as you are requesting

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive

Hope this helps a bit

CodePudding user response:

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)]

CodePudding user response:

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);
}
  • Related