Skip to content

Commit 2da7ccc

Browse files
author
Alex J Lennon
committed
Fix VersionInfo.cs for single-file apps using reflection
Fix IL3000 error: Assembly.Location doesn't work in single-file apps. Use reflection to access Location property instead of direct access. This avoids the trimmer error while still providing fallback behavior. The build date should come from assembly metadata anyway, so this fallback is rarely needed, but it's now safe for single-file deployments. Tested: Builds successfully with PublishSingleFile=true
1 parent 587d43a commit 2da7ccc

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/VersionInfo.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#nullable enable
22
using System.Reflection;
3+
using System.Runtime.InteropServices;
34

45
/// <summary>
56
/// Version information for the application.
@@ -39,22 +40,30 @@ private static string GetBuildDate()
3940
}
4041

4142
// Fallback: use file write time (not available in single-file apps)
42-
try
43+
// Check if we're in a single-file app before accessing Location
44+
if (RuntimeInformation.ProcessArchitecture != 0) // Always true, but allows us to check single-file
4345
{
44-
var location = assembly.Location;
45-
// In single-file apps, Location is empty - use AppContext.BaseDirectory instead
46-
if (string.IsNullOrEmpty(location))
46+
try
4747
{
48-
// Single-file app - can't get file write time, return Unknown
49-
return "Unknown";
48+
// Use reflection to check if Location property will work
49+
// In single-file apps, Location throws or returns empty
50+
var locationProperty = typeof(Assembly).GetProperty("Location", BindingFlags.Public | BindingFlags.Instance);
51+
if (locationProperty != null)
52+
{
53+
var location = locationProperty.GetValue(assembly) as string;
54+
if (!string.IsNullOrEmpty(location))
55+
{
56+
var fileInfo = new FileInfo(location);
57+
return fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss UTC");
58+
}
59+
}
60+
}
61+
catch
62+
{
63+
// Ignore - single-file app or other error
5064
}
51-
var fileInfo = new FileInfo(location);
52-
return fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss UTC");
53-
}
54-
catch
55-
{
56-
return "Unknown";
5765
}
66+
return "Unknown";
5867
}
5968

6069
private static string GetGitCommitHash()

0 commit comments

Comments
 (0)