66// ================================================================================
77
88using System . Reflection ;
9+ using System . Runtime . Versioning ;
910using ThingsLibrary . DataType . Extensions ;
1011
11- namespace ThingsLibrary . Base . DataType . Extensions
12+ namespace ThingsLibrary . DataType . Extensions
1213{
1314 public static class AssemblyExtensions
1415 {
16+ /// <summary>
17+ /// Agent String based on Assembly Product Name/Version and OS Version
18+ /// </summary>
19+ /// <param name="assembly">Assembly</param>
20+ /// <returns></returns>
21+ /// <exception cref="ArgumentNullException"></exception>
1522 public static string AgentString ( this Assembly assembly )
1623 {
1724 ArgumentNullException . ThrowIfNull ( assembly ) ;
@@ -22,5 +29,252 @@ public static string AgentString(this Assembly assembly)
2229
2330 return $ "{ name } /{ version . ToDotString ( ) } ({ Metrics . MachineMetrics . OsVersion ( ) } )";
2431 }
32+
33+ /// <summary>
34+ /// Get the ID from the Guid Attribute property of the assembly is available.. Guid.Empty it not found
35+ /// </summary>
36+ /// <param name="assembly">Assembly</param>
37+ /// <returns>Guid of assembly, Guid.Empty is not found</returns>
38+ public static Guid GetId ( this Assembly assembly )
39+ {
40+ // Requires an AssemblyInfo.cs with a [assembly: Guid("11111111-1111-1111-1111-111111111111")] style attribute
41+
42+ var guid = assembly . GetCustomAttribute < System . Runtime . InteropServices . GuidAttribute > ( ) ;
43+ if ( guid == null ) { return Guid . Empty ; }
44+
45+ return Guid . Parse ( guid . Value ) ;
46+ }
47+
48+ /// <summary>
49+ /// Name (same as Title)
50+ /// </summary>
51+ /// <param name="assembly">Assembly</param>
52+ /// <returns></returns>
53+ public static string Name ( this Assembly assembly ) => Title ( assembly ) ;
54+
55+ /// <summary>
56+ /// Title
57+ /// </summary>
58+ /// <param name="assembly">Assembly</param>
59+ /// <returns></returns>
60+ public static string Title ( this Assembly assembly )
61+ {
62+ ArgumentNullException . ThrowIfNull ( assembly ) ;
63+
64+ return assembly . GetCustomAttribute < AssemblyTitleAttribute > ( ) ? . Title ?? string . Empty ;
65+ }
66+
67+ /// <summary>
68+ /// Namespace
69+ /// </summary>
70+ /// <param name="assembly">Assembly</param>
71+ /// <returns></returns>
72+ public static string Namespace ( this Assembly assembly )
73+ {
74+ ArgumentNullException . ThrowIfNull ( assembly ) ;
75+
76+ return assembly . GetName ( ) ? . Name ?? string . Empty ;
77+ }
78+
79+ /// <summary>
80+ /// Version as a string
81+ /// </summary>
82+ /// <param name="assembly">Assembly</param>
83+ /// <returns></returns>
84+ public static string Version ( this Assembly assembly )
85+ {
86+ ArgumentNullException . ThrowIfNull ( assembly ) ;
87+
88+ return assembly . GetCustomAttribute < AssemblyFileVersionAttribute > ( ) ? . Version ?? string . Empty ;
89+ }
90+
91+ /// <summary>
92+ /// File Version as a Version data type
93+ /// </summary>
94+ /// <param name="assembly">Assembly</param>
95+ /// <returns></returns>
96+ public static Version FileVersion ( this Assembly assembly )
97+ {
98+ ArgumentNullException . ThrowIfNull ( assembly ) ;
99+ return assembly . GetName ( ) ? . Version ?? new System . Version ( ) ;
100+ }
101+
102+ /// <summary>
103+ /// File version as a string
104+ /// </summary>
105+ /// <param name="assembly">Assembly</param>
106+ /// <returns></returns>
107+ public static string FileVersionStr ( this Assembly assembly )
108+ {
109+ var version = assembly . FileVersion ( ) ;
110+ if ( version == null ) { return "0" ; }
111+
112+ return version . ToDotString ( ) ;
113+ }
114+
115+ /// <summary>
116+ /// File Version as a long data type
117+ /// </summary>
118+ /// <param name="assembly">Assembly</param>
119+ /// <returns></returns>
120+ public static long FileVersionLong ( this Assembly assembly ) => assembly . FileVersion ( ) . ToLong ( ) ;
121+
122+ /// <summary>
123+ /// Product Name
124+ /// </summary>
125+ /// <param name="assembly">Assembly</param>
126+ /// <returns></returns>
127+ public static string ProductName ( this Assembly assembly )
128+ {
129+ ArgumentNullException . ThrowIfNull ( assembly ) ;
130+ return assembly . GetCustomAttribute < AssemblyProductAttribute > ( ) ? . Product ?? string . Empty ;
131+ }
132+
133+ public static string Company ( this Assembly assembly )
134+ {
135+ ArgumentNullException . ThrowIfNull ( assembly ) ;
136+ return assembly . GetCustomAttribute < AssemblyCompanyAttribute > ( ) ? . Company ?? string . Empty ;
137+ }
138+
139+ /// <summary>
140+ /// Retrieves the description text specified in the assembly's <see cref="AssemblyDescriptionAttribute"/>.
141+ /// </summary>
142+ /// <remarks>This method returns the value of the <see cref="AssemblyDescriptionAttribute"/>
143+ /// applied to the specified assembly. If the attribute is not present, an empty string is returned.</remarks>
144+ /// <param name="assembly">The assembly from which to obtain the description. Cannot be <see langword="null"/>.</param>
145+ /// <returns>A string containing the description of the assembly, or an empty string if no description is defined.</returns>
146+ public static string Description ( this Assembly assembly )
147+ {
148+ ArgumentNullException . ThrowIfNull ( assembly ) ;
149+ return assembly . GetCustomAttribute < AssemblyDescriptionAttribute > ( ) ? . Description ?? string . Empty ;
150+ }
151+
152+
153+ /// <summary>
154+ /// Copyright
155+ /// </summary>
156+ /// <param name="assembly">Assembly</param>
157+ /// <returns></returns>
158+ public static string Copyright ( this Assembly assembly )
159+ {
160+ ArgumentNullException . ThrowIfNull ( assembly ) ;
161+ return assembly . GetCustomAttribute < AssemblyCopyrightAttribute > ( ) ? . Copyright ?? string . Empty ;
162+ }
163+
164+ /// <summary>
165+ /// DotNet Framework Version
166+ /// </summary>
167+ /// <param name="assembly">Assembly</param>
168+ /// <returns></returns>
169+ public static string NetFrameworkVersion ( this Assembly assembly )
170+ {
171+ ArgumentNullException . ThrowIfNull ( assembly ) ;
172+ return assembly . GetCustomAttribute < TargetFrameworkAttribute > ( ) ? . FrameworkName ?? string . Empty ;
173+ }
174+
175+ /// <summary>
176+ /// Assembly creation date
177+ /// </summary>
178+ /// <param name="assembly">Assembly</param>
179+ /// <returns></returns>
180+ public static DateTime CreatedOn ( this Assembly assembly )
181+ {
182+ ArgumentNullException . ThrowIfNull ( assembly ) ;
183+ return File . GetCreationTimeUtc ( assembly . Location ) ;
184+ }
185+
186+ /// <summary>
187+ /// Assembly last written / updated date
188+ /// </summary>
189+ /// <param name="assembly"></param>
190+ /// <returns></returns>
191+ public static DateTime UpdatedOn ( this Assembly assembly )
192+ {
193+ ArgumentNullException . ThrowIfNull ( assembly ) ;
194+ return File . GetLastWriteTimeUtc ( assembly . Location ) ;
195+ }
196+
197+ /// <summary>
198+ /// Gets the file system path of the specified assembly.
199+ /// </summary>
200+ /// <remarks>This method returns the value of the <see cref="Assembly.Location"/> property. If the
201+ /// assembly was loaded from a byte array rather than a file, the returned path will be an empty
202+ /// string.</remarks>
203+ /// <param name="assembly">The assembly for which to retrieve the file system path. Cannot be null.</param>
204+ /// <returns>The full path to the file that contains the assembly. Returns an empty string if the assembly was loaded
205+ /// from a byte array.</returns>
206+ public static string Path ( this Assembly assembly )
207+ {
208+ ArgumentNullException . ThrowIfNull ( assembly ) ;
209+ return assembly . Location ;
210+ }
211+
212+ /// <summary>
213+ /// Directory of the executing assembly
214+ /// </summary>
215+ /// <param name="assembly">Assembly</param>
216+ /// <returns></returns>
217+ public static string DirectoryPath ( this Assembly assembly )
218+ {
219+ ArgumentNullException . ThrowIfNull ( assembly ) ;
220+ return System . IO . Path . GetDirectoryName ( assembly . Location ) ?? string . Empty ;
221+ }
222+
223+ /// <summary>
224+ /// Application Data Path
225+ /// </summary>
226+ /// <param name="assembly"></param>
227+ /// <returns></returns>
228+ public static string AppDataPath ( this Assembly assembly )
229+ {
230+ ArgumentNullException . ThrowIfNull ( assembly ) ;
231+ var appDataPath = System . IO . Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . ApplicationData ) , assembly . ProductName ( ) ) ;
232+
233+ if ( ! Directory . Exists ( appDataPath ) )
234+ {
235+ Directory . CreateDirectory ( appDataPath ) ;
236+ }
237+
238+ return appDataPath ;
239+ }
240+
241+ /// <summary>
242+ /// Temp directory path (temp path + product name)
243+ /// </summary>
244+ /// <param name="assembly">Assembly</param>
245+ /// <returns></returns>
246+ public static string TempDirectoryPath ( this Assembly assembly )
247+ {
248+ ArgumentNullException . ThrowIfNull ( assembly ) ;
249+
250+ var tempPath = System . IO . Path . Combine ( System . IO . Path . GetTempPath ( ) , assembly . ProductName ( ) ) ;
251+ if ( ! Directory . Exists ( tempPath ) )
252+ {
253+ Directory . CreateDirectory ( tempPath ) ;
254+ }
255+ return tempPath ;
256+ }
257+
258+ /// <summary>
259+ /// Dependency Versions
260+ /// </summary>
261+ /// <param name="assembly">Assembly</param>
262+ /// <returns></returns>
263+ public static IDictionary < string , string > DependencyVersions ( this Assembly assembly )
264+ {
265+ ArgumentNullException . ThrowIfNull ( assembly ) ;
266+
267+ var references = assembly . GetReferencedAssemblies ( ) ;
268+
269+ var dict = new Dictionary < string , string > ( ) ;
270+ foreach ( var reference in references )
271+ {
272+ if ( string . IsNullOrEmpty ( reference . Name ) ) { continue ; } // todo? not sure what should happen here.
273+
274+ dict [ reference . Name ] = reference . Version ? . ToString ( ) ?? string . Empty ;
275+ }
276+ return dict ;
277+ }
278+
25279 }
26280}
0 commit comments