-
Notifications
You must be signed in to change notification settings - Fork 0
/
NullCheckExtensions.cs
39 lines (33 loc) · 1.16 KB
/
NullCheckExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: rimuru.dev@gmail.com
// - GitHub: https://github.com/RimuruDev
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub Organizations: https://github.com/Rimuru-Dev
//
// **************************************************************** //
using System;
using System.Diagnostics;
using Debug = UnityEngine.Debug;
namespace RimuruDev
{
public static class NullCheckExtensions
{
public static T IfNotNull<T>(this T obj, Action<T> action) where T : class
{
if (obj != null)
{
action?.Invoke(obj);
}
else
{
var stackTrace = new StackTrace();
var methodCall = stackTrace.GetFrame(1).GetMethod();
Debug.LogWarning($"Null reference at method: {methodCall.Name} in {methodCall.DeclaringType?.Name}, line: {new StackFrame(1, true).GetFileLineNumber()}");
}
return obj;
}
}
}