Unable to call Assembly.GetName() from my Silverlight application

拟墨画扇 提交于 2019-12-23 09:31:56

问题


I want to display my application version number within my application, and the simplest way to do this is to use the version number for the assembly.

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

I can get the executing assembling without problem, but the call to GetName() returns a MethodAccessException with this message

Attempt by security transparent method 'MainPage..ctor()' to access security critical method 'System.Reflection.Assembly.GetName()' failed.

Why is this happening, is there anything I can do about it, and if not is there another means of retrieving the assembly version?


回答1:


The Assembly.GetName is marked with SecurityCriticalAttribute attribute try using GetCallingAssembly().FullName and scrape the version info out of it.

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]

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




回答2:


I got this from Stackoverflow (Getting runtime version of a Silverlight 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;
    }


来源:https://stackoverflow.com/questions/7925141/unable-to-call-assembly-getname-from-my-silverlight-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!