Browse Source

Add version-independent helper for retrieving assembly attributes

ghorsington 3 years ago
parent
commit
28671ceb6f
2 changed files with 13 additions and 3 deletions
  1. 12 1
      BepInEx.Core/Contract/Attributes.cs
  2. 1 2
      BepInEx.Core/Paths.cs

+ 12 - 1
BepInEx.Core/Contract/Attributes.cs

@@ -2,8 +2,8 @@
 using System.Collections.Generic;
 using System.IO;
 using System.Linq;
+using System.Reflection;
 using BepInEx.Bootstrap;
-using BepInEx.Core;
 using Mono.Cecil;
 
 namespace BepInEx
@@ -270,6 +270,17 @@ namespace BepInEx
 		}
 
 		/// <summary>
+		/// Gets the specified attributes of an assembly, if they exist.
+		/// </summary>
+		/// <param name="assembly">The assembly.</param>
+		/// <typeparam name="T">The attribute type to retrieve.</typeparam>
+		/// <returns>The attributes of the type, if existing.</returns>
+		public static T[] GetAttributes<T>(Assembly assembly) where T : Attribute
+		{
+			return (T[])assembly.GetCustomAttributes(typeof(T), true);
+		}
+
+		/// <summary>
 		/// Gets the specified attributes of an instance, if they exist.
 		/// </summary>
 		/// <typeparam name="T">The attribute type to retrieve.</typeparam>

+ 1 - 2
BepInEx.Core/Paths.cs

@@ -1,5 +1,4 @@
 using System.IO;
-using System.Linq;
 using System.Reflection;
 using MonoMod.Utils;
 
@@ -34,7 +33,7 @@ namespace BepInEx
 			PluginPath = Utility.CombinePaths(BepInExRootPath, pluginPath);
 		}
 
-		public static SemVer.Version BepInExVersion { get; } = SemVer.Version.Parse(typeof(Paths).Assembly.GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false).Cast<AssemblyInformationalVersionAttribute>().First().InformationalVersion);
+		public static SemVer.Version BepInExVersion { get; } = SemVer.Version.Parse(MetadataHelper.GetAttributes<AssemblyInformationalVersionAttribute>(typeof(Paths).Assembly)[0].InformationalVersion);
 
 		/// <summary>
 		///     The directory that the core BepInEx DLLs reside in.