diff --git a/PowerKit.Tests/AssemblyExtensionsTests.cs b/PowerKit.Tests/AssemblyExtensionsTests.cs new file mode 100644 index 0000000..1074520 --- /dev/null +++ b/PowerKit.Tests/AssemblyExtensionsTests.cs @@ -0,0 +1,22 @@ +using System.Reflection; +using FluentAssertions; +using PowerKit.Extensions; +using Xunit; + +namespace PowerKit.Tests; + +public class AssemblyExtensionsTests +{ + [Fact] + public void TryGetVersionString_Test() + { + // Arrange + var assembly = typeof(AssemblyExtensionsTests).Assembly; + + // Act + var version = assembly.TryGetVersionString(); + + // Assert + version.Should().NotBeNullOrWhiteSpace(); + } +} diff --git a/PowerKit/Extensions/AssemblyExtensions.cs b/PowerKit/Extensions/AssemblyExtensions.cs new file mode 100644 index 0000000..a123680 --- /dev/null +++ b/PowerKit/Extensions/AssemblyExtensions.cs @@ -0,0 +1,19 @@ +using System.Reflection; + +namespace PowerKit.Extensions; + +internal static class AssemblyExtensions +{ + extension(Assembly assembly) + { + /// + /// Returns the informational version string of the assembly, falling back to the + /// assembly version if the is not set. + /// Returns if neither is available. + /// + public string? TryGetVersionString() => + assembly + .GetCustomAttribute() + ?.InformationalVersion ?? assembly.GetName().Version?.ToString(); + } +}