1+ using System ;
2+ using System . ComponentModel ;
3+ using System . IO ;
4+ using System . Reflection ;
5+
6+ namespace HexPrintFile
7+ {
8+ static class Extensions
9+ {
10+ /// <summary>
11+ /// Extension Method: Return the DateTime for the build from a given assembly file
12+ /// </summary>
13+ /// <param name="assembly">Assembly</param>
14+ /// <returns>DateTime</returns>
15+ public static DateTime GetLinkerTimestampUtc ( this Assembly assembly )
16+ {
17+ var location = assembly . Location ;
18+ return GetLinkerTimestampUtcLocation ( location ) ;
19+ }
20+
21+ /// <summary>
22+ /// Return the DateTime for the build from a given assembly file
23+ /// </summary>
24+ /// <param name="filePath">Assembly file location</param>
25+ /// <returns>string</returns>
26+ private static DateTime GetLinkerTimestampUtcLocation ( string filePath )
27+ {
28+ const int peHeaderOffset = 60 ;
29+ const int linkerTimestampOffset = 8 ;
30+ var bytes = new byte [ 2048 ] ;
31+
32+ using ( var file = new FileStream ( filePath , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
33+ {
34+ file . Read ( bytes , 0 , bytes . Length ) ;
35+ }
36+
37+ var headerPos = BitConverter . ToInt32 ( bytes , peHeaderOffset ) ;
38+ var secondsSince1970 = BitConverter . ToInt32 ( bytes , headerPos + linkerTimestampOffset ) ;
39+ var dt = new DateTime ( 1970 , 1 , 1 , 0 , 0 , 0 , DateTimeKind . Utc ) ;
40+ return dt . AddSeconds ( secondsSince1970 ) ;
41+ }
42+
43+ /// <summary>
44+ /// Extension Method to get the description attribute from an Enum value
45+ /// </summary>
46+ /// <param name="value">Enum value</param>
47+ /// <returns>string</returns>
48+ public static string GetDescription ( this Enum value )
49+ {
50+ Type type = value . GetType ( ) ;
51+ string name = Enum . GetName ( type , value ) ;
52+ if ( name != null )
53+ {
54+ FieldInfo field = type . GetField ( name ) ;
55+ if ( field != null )
56+ {
57+ DescriptionAttribute attr =
58+ Attribute . GetCustomAttribute ( field ,
59+ typeof ( DescriptionAttribute ) ) as DescriptionAttribute ;
60+ if ( attr != null )
61+ {
62+ return attr . Description ;
63+ }
64+ }
65+ }
66+ return null ;
67+ }
68+ }
69+ }
0 commit comments