Files
Common-VS2019/SacsStandardBase/Base/Utils/SysUtil.cs
T
2024-02-27 15:45:02 +00:00

84 lines
2.7 KiB
C#

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Sarand.Net.Standard.Base.Utils
{
//=======================================================================
public static class SysUtil
{
//-------------------------------------------------------------------
/// <summary>Get calling code's class name.</summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCallerClassName()
{
try
{
var stackFrame = new StackTrace().GetFrame(1);
var methodInfo = stackFrame.GetMethod();
return methodInfo.ReflectedType.Name.TrimWhitespace();
}
catch (Exception) { }
return "";
}
/// <summary>Get calling code's fucntion name.</summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCallerMemberName([CallerMemberName] string memberName = "")
{
return memberName;
}
/// <summary>Get calling code's file path.</summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCallerFilePath([CallerFilePath] string filePath = "")
{
return filePath;
}
/// <summary>Get calling code's line number.</summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static int GetCallerLineNumber([CallerLineNumber] int lineNumber = 0)
{
return lineNumber;
}
//-------------------------------------------------------------------
/// <summary>Shortcut to GetCallerClassMethod() - returns [class.method] for calling code.</summary>
public static string Fn { get { return GetCallerClassMethod(2); } }
/// <summary>Return identity of calling method as [class.method].</summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCallerClassMethod(int stackFrameIdx = 1)
{
try
{
var stackFrame = new StackTrace().GetFrame(stackFrameIdx);
var methodInfo = stackFrame.GetMethod();
return (methodInfo.ReflectedType.Name + "." + methodInfo.Name).TrimWhitespace().Trim('.');
}
catch (Exception) { }
return "";
}
//-------------------------------------------------------------------
/// <summary>Return identity of calling method as [class.method:line].</summary>
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCallerClassMethodLine()
{
try
{
var stackFrame = new StackTrace().GetFrame(1);
var methodInfo = stackFrame.GetMethod();
return (methodInfo.ReflectedType.Name + "." + methodInfo.Name + ":" + stackFrame.GetFileLineNumber()).TrimWhitespace().Trim('.');
}
catch (Exception) { }
return "";
}
//-------------------------------------------------------------------
}
//=======================================================================
}