-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAssemblyInfo.cs
103 lines (66 loc) · 2.98 KB
/
AssemblyInfo.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
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace Gsemac.Reflection {
public class AssemblyInfo :
IAssemblyInfo {
// Public members
public string Description => GetDescription();
public string Directory => GetDirectory();
public string FileName => GetFileName();
public string Location => GetLocation();
public string Name => GetName();
public string ProductName => GetProductName();
public Version ProductVersion => GetProductVersion();
public Version Version => GetVersion();
public static AssemblyInfo CallingAssembly => new AssemblyInfo(Assembly.GetCallingAssembly());
public static AssemblyInfo EntryAssembly => Assembly.GetEntryAssembly() is null ? null : new AssemblyInfo(Assembly.GetEntryAssembly()); // GetEntryAssembly() returns null in some contexts (e.g. from unit testing frameworks)
public static AssemblyInfo ExecutingAssembly => new AssemblyInfo(Assembly.GetExecutingAssembly());
public AssemblyInfo(Assembly assembly) {
if (assembly is null)
throw new ArgumentNullException(nameof(assembly));
this.assembly = assembly;
this.fileVersionInfo = new Lazy<FileVersionInfo>(GetFileVersionInfo);
}
// Private members
private readonly Assembly assembly;
private readonly Lazy<FileVersionInfo> fileVersionInfo;
private FileVersionInfo GetFileVersionInfo() {
if (System.IO.File.Exists(Location))
return FileVersionInfo.GetVersionInfo(Location);
return null;
}
private string GetDescription() {
AssemblyDescriptionAttribute descriptionAttribute = Assembly.GetEntryAssembly()
.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false)
.OfType<AssemblyDescriptionAttribute>()
.FirstOrDefault();
return descriptionAttribute?.Description ?? string.Empty;
}
private string GetDirectory() {
return System.IO.Path.GetDirectoryName(Location);
}
private string GetFileName() {
return System.IO.Path.GetFileName(Location);
}
private string GetLocation() {
return assembly.Location;
}
private string GetName() {
return assembly.GetName().Name;
}
private string GetProductName() {
return fileVersionInfo.Value?.ProductName ?? Name;
}
private Version GetProductVersion() {
string productVersionStr = fileVersionInfo.Value?.ProductVersion;
if (!string.IsNullOrWhiteSpace(productVersionStr) && Version.TryParse(productVersionStr, out Version parsedProductVersion))
return parsedProductVersion;
return Version;
}
private Version GetVersion() {
return assembly.GetName().Version;
}
}
}