This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
CallingAssembly.cs
118 lines (93 loc) · 3.53 KB
/
CallingAssembly.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading;
using BaselineTypeDiscovery;
[assembly: IgnoreAssembly]
namespace BaselineTypeDiscovery
{
/// <summary>
/// Use to walk up the execution stack and "find" the assembly
/// that originates the call. Ignores system assemblies and any
/// assembly marked with the [IgnoreOnScanning] attribute
/// </summary>
public class CallingAssembly
{
/// <summary>
/// Method is used to get the stack trace in english
/// </summary>
/// <returns>Stack trace in english</returns>
private static string GetStackTraceInEnglish()
{
var currentUiCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
string trace = Environment.StackTrace;
Thread.CurrentThread.CurrentUICulture = currentUiCulture;
return trace;
}
public static Assembly Find()
{
string trace = GetStackTraceInEnglish();
var parts = trace.Split('\n');
for (int i = 0; i < parts.Length; i++)
{
var line = parts[i];
var assembly = findAssembly(line);
if (assembly != null && !isSystemAssembly(assembly))
{
return assembly;
}
}
return Assembly.GetEntryAssembly();
}
private static string[] _prefixesToIgnore = new []{"System.", "Microsoft."};
private static bool isSystemAssembly(Assembly assembly)
{
if (assembly == null) return false;
if (assembly.GetCustomAttributes<IgnoreAssemblyAttribute>().Any()) return true;
var assemblyName = assembly.GetName().Name;
return isSystemAssembly(assemblyName);
}
private static bool isSystemAssembly(string assemblyName)
{
return _prefixesToIgnore.Any(x => assemblyName.StartsWith(x));
}
private static readonly IList<string> _misses = new List<string>();
private static Assembly findAssembly(string stacktraceLine)
{
var candidate = stacktraceLine.Trim().Substring(3);
// Short circuit this
if (isSystemAssembly(candidate)) return null;
Assembly assembly = null;
var names = candidate.Split('.');
for (var i = names.Length - 2; i > 0; i--)
{
var possibility = string.Join(".", names.Take(i).ToArray());
if (_misses.Contains(possibility)) continue;
try
{
assembly = Assembly.Load(new AssemblyName(possibility));
break;
}
catch
{
_misses.Add(possibility);
}
}
return assembly;
}
/// <summary>
/// Finds the calling assembly from the specified type
/// </summary>
/// <param name="registry"></param>
/// <returns></returns>
public static Assembly DetermineApplicationAssembly(object registry)
{
if (registry == null) throw new ArgumentNullException(nameof(registry));
var assembly = registry.GetType().Assembly;
return isSystemAssembly(assembly) ? Find() : assembly;
}
}
}