本文介绍了如何获取版本属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请让我知道为什么在获取版本时引发NullReferenceException
private static void showBanner()
{
string text =
@
_title_:_description_ Ver._version_
_copyright_
此_title_根据ISC许可证的条款分发。
(http://www.isc.org/downloads/software-support-policy/isc-license/)
;
string title = getAttribute< AssemblyTitleAttribute>()。Title;
string description = getAttribute< AssemblyDescriptionAttribute>()。
string version = getAttribute< AssemblyVersionAttribute>()。版本;
string copyright = getAttribute< AssemblyCopyrightAttribute>()。版权;
text = text.Replace( _ title _,title)
.Replace ( _ description _,description)
.Replace( _ version _,version)
.Replace( _ copyright _,copyright);
Console.Error.WriteLine(text);
}
private static Tattribute getAttribute< Tattribute>() 其中 Tattribute:Attribute
{
Assembly assembly = Assembly.GetExecutingAssembly();
return (Tattribute)Attribute.GetCustomAttribute( assembly , typeof运算跨度>(Tattribute));
}
我的尝试:
两者都是相同的结果
[ assembly :AssemblyVersion( 1.0。*)]
[ assembly :AssemblyVersion( 1.0.0.0)]
这段代码也一样,所以我认为这似乎不是模板问题。
string version =((AssemblyVersionAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof (AssemblyVersionAttribute)))。版本;
我可以通过此代码获得版本。
string version = Assembly.GetExecutingAssembly( ).GetName()。Version.ToString();
解决方案
string version = Assembly.GetExecutingAssembly()。GetName()。Version.ToString();
:
>要获取已加载的程序集的名称,请在程序集上调用GetName以获取AssemblyName,然后获取Version属性。
please let me know why NullReferenceException is raised at getting version
private static void showBanner() { string text = @" _title_ : _description_ Ver._version_ _copyright_ This ""_title_"" is distributed under terms of the ISC License. ( http://www.isc.org/downloads/software-support-policy/isc-license/ ) "; string title = getAttribute<AssemblyTitleAttribute>().Title; string description = getAttribute<AssemblyDescriptionAttribute>().Description; string version = getAttribute<AssemblyVersionAttribute>().Version; string copyright = getAttribute<AssemblyCopyrightAttribute>().Copyright; text = text.Replace("_title_", title) .Replace("_description_", description) .Replace("_version_", version) .Replace("_copyright_", copyright); Console.Error.WriteLine(text); } private static Tattribute getAttribute<Tattribute>() where Tattribute : Attribute { Assembly assembly = Assembly.GetExecutingAssembly(); return (Tattribute)Attribute.GetCustomAttribute(assembly, typeof(Tattribute)); }
What I have tried:
both were same result
[assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")]
this code is same too, so I think that does not seem a Template problem.
string version = ((AssemblyVersionAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyVersionAttribute))).Version;
I can get version by this code.
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
解决方案
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
https://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.aspx:
> To get the name of an assembly you have loaded, call GetName on the assembly to get an AssemblyName, and then get the Version property.
这篇关于如何获取版本属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!