-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathReflectionUtility.cs
More file actions
128 lines (108 loc) · 4.14 KB
/
ReflectionUtility.cs
File metadata and controls
128 lines (108 loc) · 4.14 KB
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
119
120
121
122
123
124
125
126
127
128
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 the ProductUrl from the assembly metadata.
/// </summary>
/// <param name="assembly">The assembly to get the ProductUrl metadata from.</param>
/// <returns>The assembly's build time as a Uri if an <see cref="AssemblyMetadataAttribute"/> is found
/// with Key="ProductUrl" and Value equal to a parsable absolute Uri. Returns null otherwise.</returns>
public static Uri? GetProductUrl(Assembly assembly)
{
Conditions.RequireReference(assembly, nameof(assembly));
Uri? result = assembly.GetCustomAttributes<AssemblyMetadataAttribute>()
.Where(metadata => string.Equals(metadata.Key, "ProductUrl"))
.Select(metadata => Uri.TryCreate(metadata.Value, UriKind.Absolute, out Uri? value) ? value : null)
.FirstOrDefault(value => value != null);
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 = assembly.GetCustomAttribute<AssemblyConfigurationAttribute>();
result = configuration?.Configuration?.Contains("Debug") ?? false;
}
return result;
}
#endregion
}
}