从静态类获取程序集版本信息会引发stackoverflow异常(Getting assembly version info from static class throws a stackoverfl

编程入门 行业动态 更新时间:2024-10-15 06:19:36
静态类获取程序集版本信息会引发stackoverflow异常(Getting assembly version info from static class throws a stackoverflow exception)

我有点挣扎,我觉得我非常接近。 在.NET MVC Web应用程序中,我曾经使用前端显示的assemblyinfo信息没有问题。 在一些优化中,我想将该代码移到一个通用的助手类。 为了便于使用,我把它变成了一个静态类,但是我在这个过程中遇到了几个障碍。 但是现在它在我尝试使用它时会抛出一个System.StackOverflowException,遗憾的是。 这是代码:

public static class VersionInformationHelper { public static string GetVersionNumber { get { if (!string.IsNullOrWhiteSpace(GetVersionNumber.GetType().Assembly.GetName().Version.ToString())) { return "v" + GetVersionNumber.GetType().Assembly.GetName().Version.ToString(); } else { return string.Empty; } } } /// <remark> /// This doesnt exactly return the commit hash so to speak. Well it does, but Teamcity is set to enter the corresponding commit hash information when building, /// into productversion in "AssemblyInfo.cs". It could be any string really. But we assume that a commit hash will always be in that location. /// It's "Assembly informational version" in the assemblyinfo patcher build feature in teamcity. /// </remark> public static string GetCommitHash { get { if (!string.IsNullOrWhiteSpace(System.Diagnostics.FileVersionInfo.GetVersionInfo(GetVersionNumber.GetType().Assembly.Location).ProductVersion)) { return System.Diagnostics.FileVersionInfo.GetVersionInfo(GetVersionNumber.GetType().Assembly.Location).ProductVersion; } else { return string.Empty; } } } public static string GetBuildDate { get { return string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:dd/MM/yy HH:mm:ss}", System.IO.File.GetLastWriteTime(GetVersionNumber.GetType().Assembly.Location)); } } }

编辑

基于反馈的固定代码(GetVersionNumber和GetCommitHash“已更改):

public static class VersionInformationHelper { public static string GetVersionNumber { get { if (!string.IsNullOrWhiteSpace(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString())) { return "v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); } else { return string.Empty; } } } /// <remark> /// This doesnt exactly return the commit hash so to speak. Well it does, but Teamcity is set to enter the corresponding commit hash information when building, /// into productversion in "AssemblyInfo.cs". It could be any string really. But we assume that a commit hash will always be in that location. /// It's "Assembly informational version" in the assemblyinfo patcher build feature in teamcity. /// </remark> public static string GetCommitHash { get { if (!string.IsNullOrWhiteSpace(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).ProductVersion)) { return System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).ProductVersion; } else { return string.Empty; } } } public static string GetBuildDate { get { return string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:dd/MM/yy HH:mm:ss}", System.IO.File.GetLastWriteTime(GetVersionNumber.GetType().Assembly.Location)); } } }

I have struggled with this for a bit, and I have a feeling I am very close. In a .NET MVC web application I have used to have assemblyinfo information displayed in the front end without issue. In a bit of optimization I wanted to move that code out to a general purpose helper class. For ease of use I have made it a static class, but I have hit several snags in the process. But now it throws a System.StackOverflowException when I try to use it, sadly. Here is the code:

public static class VersionInformationHelper { public static string GetVersionNumber { get { if (!string.IsNullOrWhiteSpace(GetVersionNumber.GetType().Assembly.GetName().Version.ToString())) { return "v" + GetVersionNumber.GetType().Assembly.GetName().Version.ToString(); } else { return string.Empty; } } } /// <remark> /// This doesnt exactly return the commit hash so to speak. Well it does, but Teamcity is set to enter the corresponding commit hash information when building, /// into productversion in "AssemblyInfo.cs". It could be any string really. But we assume that a commit hash will always be in that location. /// It's "Assembly informational version" in the assemblyinfo patcher build feature in teamcity. /// </remark> public static string GetCommitHash { get { if (!string.IsNullOrWhiteSpace(System.Diagnostics.FileVersionInfo.GetVersionInfo(GetVersionNumber.GetType().Assembly.Location).ProductVersion)) { return System.Diagnostics.FileVersionInfo.GetVersionInfo(GetVersionNumber.GetType().Assembly.Location).ProductVersion; } else { return string.Empty; } } } public static string GetBuildDate { get { return string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:dd/MM/yy HH:mm:ss}", System.IO.File.GetLastWriteTime(GetVersionNumber.GetType().Assembly.Location)); } } }

EDIT

Fixed code based on feedback (GetVersionNumber and GetCommitHash" has been changed):

public static class VersionInformationHelper { public static string GetVersionNumber { get { if (!string.IsNullOrWhiteSpace(System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString())) { return "v" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); } else { return string.Empty; } } } /// <remark> /// This doesnt exactly return the commit hash so to speak. Well it does, but Teamcity is set to enter the corresponding commit hash information when building, /// into productversion in "AssemblyInfo.cs". It could be any string really. But we assume that a commit hash will always be in that location. /// It's "Assembly informational version" in the assemblyinfo patcher build feature in teamcity. /// </remark> public static string GetCommitHash { get { if (!string.IsNullOrWhiteSpace(System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).ProductVersion)) { return System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).ProductVersion; } else { return string.Empty; } } } public static string GetBuildDate { get { return string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:dd/MM/yy HH:mm:ss}", System.IO.File.GetLastWriteTime(GetVersionNumber.GetType().Assembly.Location)); } } }

最满意答案

您正在从相同的GetVersionNumber (两次)的getter中读取GetVersionNumber 。 这将无限循环(或直到发生堆栈溢出)。

您可能需要使用Assembly.GetExecutingAssembly().GetName().Version.ToString()或另一种获取版本的方法来更改两个出现的位置。

You are reading GetVersionNumber from the getter of same GetVersionNumber (twice). This will loop infinitely (or until a stack overflow occurs).

You probably need to change the two occurences with Assembly.GetExecutingAssembly().GetName().Version.ToString() or another method to get the version.

更多推荐

本文发布于:2023-07-15 17:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1116648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   异常   版本   程序   信息

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!