2017-06-29 41 views
0

我一直在努力与此有点,我有一种感觉我非常接近。在.NET MVC Web应用程序中,我曾经在前端显示汇编信息信息而没有问题。在一些优化中,我想将这些代码移出到通用帮助类。 为了便于使用,我将它定义为一个静态类,但是我在这个过程中遇到了一些问题。但是现在,当我尝试使用它时抛出一个System.StackOverflowException,可悲的是。下面是代码:从静态类获取程序集版本信息抛出一个stackoverflow例外

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)); 
     } 
    } 
} 
+1

备注:财产形式的方法是一个坏主意,因为一般来说他们没有得到优化;最好保留属性作为属性,如果它是一种方法,它应该是一种方法 –

+1

好信息,我会牢记:) –

回答

1

您从同一吸气阅读GetVersionNumberGetVersionNumber(两次),这将无限循环(或直到发生堆栈溢出)。

您可能需要使用Assembly.GetExecutingAssembly().GetName().Version.ToString()或另一种获取版本的方法更改这两个事件。

+0

谢谢你的回复,按照你的提示我做了一些修改,它似乎工作:)我用固定代码更新了我的原始帖子。 –

相关问题