| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Injection.AssemblyOrdering; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides a simplified view of assembly information for ordering expressions. |
| | | 7 | | /// This abstraction works for both reflection (runtime Assembly) and source-gen (compile-time info). |
| | | 8 | | /// </summary> |
| | | 9 | | public sealed class AssemblyInfo |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// The assembly name (without extension). |
| | | 13 | | /// </summary> |
| | 372 | 14 | | public string Name { get; } |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// The full file path/location of the assembly (if available). |
| | | 18 | | /// </summary> |
| | 27 | 19 | | public string Location { get; } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Creates an AssemblyInfo from a runtime Assembly. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <remarks> |
| | | 25 | | /// Note: In single-file published apps, Assembly.Location returns an empty string. |
| | | 26 | | /// This is expected behavior and the Location property will be empty in those scenarios. |
| | | 27 | | /// </remarks> |
| | | 28 | | public static AssemblyInfo FromAssembly(Assembly assembly) |
| | | 29 | | { |
| | 153 | 30 | | ArgumentNullException.ThrowIfNull(assembly); |
| | | 31 | | // Note: Assembly.Location returns empty string in single-file apps. |
| | | 32 | | // This is documented behavior and acceptable - Location is optional. |
| | | 33 | | #pragma warning disable IL3000 // 'Assembly.Location' always returns empty string for single-file apps |
| | 153 | 34 | | return new AssemblyInfo( |
| | 153 | 35 | | assembly.GetName().Name ?? string.Empty, |
| | 153 | 36 | | assembly.Location); |
| | | 37 | | #pragma warning restore IL3000 |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Creates an AssemblyInfo from string values (for source-gen scenarios). |
| | | 42 | | /// </summary> |
| | | 43 | | public static AssemblyInfo FromStrings(string name, string location = "") |
| | | 44 | | { |
| | 29 | 45 | | return new AssemblyInfo(name, location); |
| | | 46 | | } |
| | | 47 | | |
| | 182 | 48 | | private AssemblyInfo(string name, string location) |
| | | 49 | | { |
| | 182 | 50 | | Name = name ?? string.Empty; |
| | 182 | 51 | | Location = location ?? string.Empty; |
| | 182 | 52 | | } |
| | | 53 | | } |