using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Sarand.Net.Standard.Base.Utils
{
//=======================================================================
public static class SysUtil
{
//-------------------------------------------------------------------
/// Get calling code's class name.
///
[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 "";
}
/// Get calling code's function name.
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCallerMemberName([CallerMemberName] string memberName = "")
{
return memberName;
}
/// Get calling code's file path.
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCallerFilePath([CallerFilePath] string filePath = "")
{
return filePath;
}
/// Get calling code's line number.
[MethodImpl(MethodImplOptions.NoInlining)]
public static int GetCallerLineNumber([CallerLineNumber] int lineNumber = 0)
{
return lineNumber;
}
//-------------------------------------------------------------------
/// Shortcut to GetCallerClassMethod() - returns [class.method] for calling code.
public static string Fn { get { return GetCallerClassMethod(2); } }
/// Return identity of calling method as [class.method].
[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 "";
}
//-------------------------------------------------------------------
/// Return identity of calling method as [class.method:line].
[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 "";
}
//-------------------------------------------------------------------
}
//=======================================================================
}