我有点挣扎,我觉得我非常接近。 在.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.
更多推荐
发布评论