我想在我的应用程序中显示我的应用程序版本号,最简单的方法是使用程序集的版本号.
var assembly = System.Reflection.Assembly.GetExecutingAssembly(); var name = assembly.GetName(); return String.Format("Version {0}.{1}",name.Version.Major,name.Version.Minor);
我可以毫无问题地获得正在执行的汇编,但是对GetName()的调用会返回带有此消息的MethodAccessException
Attempt by security transparent method ‘MainPage..ctor()’ to access security critical method ‘System.Reflection.Assembly.GetName()’ Failed.
为什么会发生这种情况,有什么我可以做的,如果没有,还有其他方法来检索汇编版本吗?
解决方法
Assembly.GetName用SecurityCriticalAttribute属性标记,尝试使用GetCallingAssembly().FullName并从中删除版本信息.
Do not use this member in your application. If you do,your code will
throw a MethodAccessException. This member is security-critical,which
restricts it to internal use by the .NET Framework for Silverlight
class library. [SECURITY CRITICAL]
来自:http://msdn.microsoft.com/en-us/library/9w2wdeze(VS.95).aspx