-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionExtensions.cs
61 lines (59 loc) · 2.07 KB
/
ReflectionExtensions.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Funcular.ExtensionMethods
{
public static class ReflectionExtensions
{
/// <summary>
/// For .NET 4.0 and below. In .NET 4.5 and later, use the
/// CallerNameAttribute instead. . Note: In release mode, unit
/// tests will fail because of test instrumentation.
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCurrentMethodName()
{
string ret = null;
var stackTrace = new StackTrace(); // get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames)
int i = 0;
// write call stack method names
if (stackFrames != null)
foreach (var stackFrame in stackFrames)
{
i++;
if (i >= 2)
{
ret = stackFrame.GetMethod().Name;
break;
}
}
return ret;
}
/// <summary>
/// For .NET 4.0 and below. In .NET 4.5 and later, use the
/// CallerNameAttribute instead. Note: In release mode, unit
/// tests will fail because of test instrumentation.
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCallingMethodName()
{
string ret = null;
var stackTrace = new StackTrace(); // get call stack
StackFrame[] stackFrames = stackTrace.GetFrames(); // get method calls (frames)
int i = 0;
if (stackFrames != null)
foreach (var stackFrame in stackFrames)
{
i++;
if (i > 2)
{
ret = stackFrame.GetMethod().Name;
break;
}
}
return ret;
}
}
}