我编写了以下函数,以便在我的统一项目中提供一个方便的实用程序来进行健全性检查。
namespace MyProject.Utilities
{
public class DebugUtils
{
/// <summary>
/// Checks if passed object is null and prints to console if it is.
/// </summary>
/// <param name="obj">The obj.</param>
public static void DebugAssertNull<T>(T obj, string tag = "M_UTILS_DEBUG")
{
if (obj == null)
{
StackTrace stackTrace = new();
StackFrame stackFrame = stackTrace.GetFrame(1);
string methodName = stackFrame.GetMethod().Name;
string fileName = stackFrame.GetFileName();
int lineNumber = stackFrame.GetFileLineNumber();
string message = $"[{tag}] Object of type {typeof(T)} is null in method {methodName} at {fileName}:{lineNumber}"; …Run Code Online (Sandbox Code Playgroud)