Failed to call Assembly.GetName () from Silverlight Application

I want to display my version number of the application in my application, and the easiest way to do this is to use the version number for the build.

var assembly = System.Reflection.Assembly.GetExecutingAssembly(); var name = assembly.GetName(); return String.Format("Version {0}.{1}", name.Version.Major, name.Version.Minor); 

I can complete the build without problems, but calling GetName() returns a MethodAccessException with this message

An attempt was made by the transparent security method "MainPage..ctor ()" to access the critical critical method "Error System.Reflection.Assembly.GetName ()".

Why is this happening, is there anything I can do about it, and if there is no other way to get the build version?

+6
source share
2 answers

Assembly.GetName is marked with the SecurityCriticalAttribute attribute, try using GetCallingAssembly().FullName and clear the version information from it.

Do not use this element in your application. If you do this, your code will throw a MethodAccessException. This member is security critical, which limits it to internal use of the .NET Framework for the Silverlight library class. [SECURITY CRITICAL]

from: http://msdn.microsoft.com/en-us/library/9w2wdeze(VS.95).aspx

+6
source

I got this from Stackoverflow ( Getting the Silverlight Runtime Version of the Assembly ) ... works for me:

  public static string GetVersion() { string versionNumber = ParseVersionNumber(Assembly.GetExecutingAssembly()).ToString(); return versionNumber; } private static Version ParseVersionNumber(Assembly assembly) { AssemblyName assemblyName = new AssemblyName(assembly.FullName); return assemblyName.Version; } 
+8
source

Source: https://habr.com/ru/post/900263/


All Articles