An application that I work on has many (in excess of 50) referenced custom .NET assemblies. Most of the custom assemblies follow the same version number scheme – but not all of them. And at either the beginning or end of a release cycle, the version numbers change and it can be difficult to track and ensure you have the proper assemblies referenced.
Red Gate’s .NET Reflector is a nice tool to examine an assembly but I need a way to plow through my bin folder and log all of my custom assemblies referenced assemblies, including the version numbers.
The code below does that. (Full disclosure, this code is a mixture of a couple of examples I found on the internet made to work for my situation.)
All of my custom assemblies have a distinct naming convention so I call the CollectAndLogReferences method with this regular expression “^(AAA|AAB|AAC)” to filter only my custom dlls and executables. With the filtering like this, I don’t log all of the system dlls and others that I don’t care about.
I paste the output (which is a lot of text) into my text editor. From there, I can easily search, sort, etc. as needed.
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
| ...
CollectAndLogReferences("^(AAA|AAB|AAC)");
public void CollectAndLogReferences(string sReferenceExpression)
{
var sRefExp = "^(.)";
if (sReferenceExpression.Length > 0)
sRefExp = sReferenceExpression;
int indent = 0;
var loggedAssembly = new List<string>();
var referencedAssembly = new List<string>();
var p = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var files = Directory.GetFiles(p, "*.*").Where(f => f.EndsWith(".exe") || f.EndsWith(".dll"));
foreach (string file in files)
{
try
{
var assembly = Assembly.LoadFile(file);
if (!Regex.IsMatch(assembly.FullName, sRefExp))
continue;
if (!referencedAssembly.Contains(assembly.FullName))
referencedAssembly.Add(assembly.FullName);
}
catch (Exception e) { }
}
while (referencedAssembly.Count > 0)
{
LogAssemblies(indent, sRefExp, loggedAssembly, referencedAssembly);
}
}
public void LogAssemblies(int indent, string sRefExp, List<string> logged, List<string> referenced)
{
Display(indent, "");
var referencedAssembly2 = new List<string>();
foreach (var refd in referenced)
{
if (logged.Contains(refd)) continue;
try
{
Assembly r = Assembly.Load(refd);
Display(indent, "Assembly: {0}", r);
Display(indent, "Referenced assemblies:");
foreach (AssemblyName an2 in r.GetReferencedAssemblies())
{
if (Regex.IsMatch(an2.Name, sRefExp))
{
Display(indent + 1, "Name={0}, Version={1}, Culture={2}, PublicKeyToken={3}",
an2.Name, an2.Version, an2.CultureInfo.Name, (BitConverter.ToString(an2.GetPublicKeyToken())));
if (!referencedAssembly2.Contains(an2.FullName))
referencedAssembly2.Add(an2.FullName);
}
}
logged.Add(r.FullName);
Display(indent, "");
}
catch (Exception e)
{
Display(indent, "EXCEPTION DUMP BEGIN * * * *");
Display(indent, refd);
Display(indent, e.ToString());
Display(indent, "EXCEPTION DUMP END * * * * *");
}
}
referenced.Clear();
referenced.AddRange(referencedAssembly2);
}
public void Display(int indent, string format, params object[] param)
{
var str = string.Format(format, param);
//Output.Text += new string(' ', indent * 2);
//Output.Text += str + Environment.NewLine;
Console.Write(new string(' ', indent * 2));
Console.WriteLine(str);
} |
...
CollectAndLogReferences("^(AAA|AAB|AAC)");
public void CollectAndLogReferences(string sReferenceExpression)
{
var sRefExp = "^(.)";
if (sReferenceExpression.Length > 0)
sRefExp = sReferenceExpression;
int indent = 0;
var loggedAssembly = new List<string>();
var referencedAssembly = new List<string>();
var p = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var files = Directory.GetFiles(p, "*.*").Where(f => f.EndsWith(".exe") || f.EndsWith(".dll"));
foreach (string file in files)
{
try
{
var assembly = Assembly.LoadFile(file);
if (!Regex.IsMatch(assembly.FullName, sRefExp))
continue;
if (!referencedAssembly.Contains(assembly.FullName))
referencedAssembly.Add(assembly.FullName);
}
catch (Exception e) { }
}
while (referencedAssembly.Count > 0)
{
LogAssemblies(indent, sRefExp, loggedAssembly, referencedAssembly);
}
}
public void LogAssemblies(int indent, string sRefExp, List<string> logged, List<string> referenced)
{
Display(indent, "");
var referencedAssembly2 = new List<string>();
foreach (var refd in referenced)
{
if (logged.Contains(refd)) continue;
try
{
Assembly r = Assembly.Load(refd);
Display(indent, "Assembly: {0}", r);
Display(indent, "Referenced assemblies:");
foreach (AssemblyName an2 in r.GetReferencedAssemblies())
{
if (Regex.IsMatch(an2.Name, sRefExp))
{
Display(indent + 1, "Name={0}, Version={1}, Culture={2}, PublicKeyToken={3}",
an2.Name, an2.Version, an2.CultureInfo.Name, (BitConverter.ToString(an2.GetPublicKeyToken())));
if (!referencedAssembly2.Contains(an2.FullName))
referencedAssembly2.Add(an2.FullName);
}
}
logged.Add(r.FullName);
Display(indent, "");
}
catch (Exception e)
{
Display(indent, "EXCEPTION DUMP BEGIN * * * *");
Display(indent, refd);
Display(indent, e.ToString());
Display(indent, "EXCEPTION DUMP END * * * * *");
}
}
referenced.Clear();
referenced.AddRange(referencedAssembly2);
}
public void Display(int indent, string format, params object[] param)
{
var str = string.Format(format, param);
//Output.Text += new string(' ', indent * 2);
//Output.Text += str + Environment.NewLine;
Console.Write(new string(' ', indent * 2));
Console.WriteLine(str);
}