forked from menees/Libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionUtility.cs
110 lines (93 loc) · 3.38 KB
/
ReflectionUtility.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
namespace Menees
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
#endregion
/// <summary>
/// Methods for getting metadata about assemblies, types, members, etc.
/// </summary>
public static class ReflectionUtility
{
#region Public Methods
/// <summary>
/// Gets the assembly's copyright information.
/// </summary>
/// <param name="assembly">The assembly to get the copyright from.</param>
/// <returns>User-friendly copyright information.</returns>
public static string? GetCopyright(Assembly assembly)
{
Conditions.RequireReference(assembly, nameof(assembly));
string? result = null;
var copyrights = (AssemblyCopyrightAttribute[])assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), true);
if (copyrights.Length > 0)
{
result = copyrights[0].Copyright;
}
return result;
}
/// <summary>
/// Gets the assembly's version.
/// </summary>
/// <param name="assembly">The assembly to get the version from</param>
/// <returns>The assembly version</returns>
public static Version? GetVersion(Assembly assembly)
{
Conditions.RequireReference(assembly, nameof(assembly));
Version? result = assembly.GetName(false).Version;
return result;
}
/// <summary>
/// Gets the UTC build timestamp from the assembly.
/// </summary>
/// <param name="assembly">The assembly to get the BuildTime metadata from.</param>
/// <returns>The assembly's build time as a UTC datetime if an <see cref="AssemblyMetadataAttribute"/> is found
/// with Key="BuildTime" and Value equal to a parsable UTC datetime. Returns null otherwise.</returns>
public static DateTime? GetBuildTime(Assembly assembly)
{
Conditions.RequireReference(assembly, nameof(assembly));
const DateTimeStyles UseUtc = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal;
DateTime? result = assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.Where(metadata => string.Equals(metadata.Key, "BuildTime"))
.Select(metadata => DateTime.TryParse(metadata.Value, null, UseUtc, out DateTime value) ? value : (DateTime?)null)
.FirstOrDefault(value => value != null);
if (result != null)
{
result = result.Value.Kind switch
{
DateTimeKind.Unspecified => DateTime.SpecifyKind(result.Value, DateTimeKind.Utc),
DateTimeKind.Local => result.Value.ToUniversalTime(),
_ => result.Value,
};
}
return result;
}
/// <summary>
/// Gets whether the assembly was built with a debug configuration.
/// </summary>
/// <param name="assembly">The assembly to check.</param>
/// <returns>True if the <see cref="AssemblyConfigurationAttribute"/> is present
/// and the configuration string contains "Debug". False otherwise.</returns>
public static bool IsDebugBuild(Assembly assembly)
{
Conditions.RequireReference(assembly, nameof(assembly));
bool result = false;
if (assembly != null)
{
var configuration = (AssemblyConfigurationAttribute?)assembly.GetCustomAttribute(typeof(AssemblyConfigurationAttribute));
result = configuration?.Configuration?.Contains("Debug") ?? false;
}
return result;
}
#endregion
}
}