-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
68 lines (63 loc) · 2.36 KB
/
Extensions.cs
File metadata and controls
68 lines (63 loc) · 2.36 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
using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
namespace HexPrintFile
{
static class Extensions
{
/// <summary>
/// Extension Method: Return the DateTime for the build from a given assembly file
/// </summary>
/// <param name="assembly">Assembly</param>
/// <returns>DateTime</returns>
public static DateTime GetLinkerTimestampUtc(this Assembly assembly)
{
return GetLinkerTimestampUtcLocation(Environment.ProcessPath);
}
/// <summary>
/// Return the DateTime for the build from a given assembly file
/// </summary>
/// <param name="filePath">Assembly file location</param>
/// <returns>string</returns>
private static DateTime GetLinkerTimestampUtcLocation(string filePath)
{
const int peHeaderOffset = 60;
const int linkerTimestampOffset = 8;
var bytes = new byte[2048];
using (var file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
file.Read(bytes, 0, bytes.Length);
}
var headerPos = BitConverter.ToInt32(bytes, peHeaderOffset);
var secondsSince1970 = BitConverter.ToInt32(bytes, headerPos + linkerTimestampOffset);
var dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return dt.AddSeconds(secondsSince1970);
}
/// <summary>
/// Extension Method to get the description attribute from an Enum value
/// </summary>
/// <param name="value">Enum value</param>
/// <returns>string</returns>
public static string GetDescription(this Enum value)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
DescriptionAttribute attr =
Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attr != null)
{
return attr.Description;
}
}
}
return null;
}
}
}