【问题标题】:Unable to call Assembly.GetName() from my Silverlight application无法从我的 Silverlight 应用程序调用 Assembly.GetName()
【发布时间】:2011-10-28 04:25:08
【问题描述】:

我想在我的应用程序中显示我的应用程序版本号,最简单的方法是使用程序集的版本号。

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

我可以毫无问题地执行组装,但是对 GetName() 的调用返回带有此消息的 MethodAccessException

尝试通过安全透明方法“MainPage..ctor()”访问安全关键方法“System.Reflection.Assembly.GetName()”失败。

为什么会发生这种情况,我能做些什么,如果没有,是否有其他方法可以检索程序集版本?

【问题讨论】:

    标签: c# silverlight reflection version methodaccessexception


    【解决方案1】:

    我从 Stackoverflow (Getting runtime version of a Silverlight assembly) 得到了这个……对我有用:

        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;
        }
    

    【讨论】:

      【解决方案2】:

      Assembly.GetName 标有 SecurityCriticalAttribute 属性,尝试使用 GetCallingAssembly().FullName 并从中刮取版本信息。

      不要在您的应用程序中使用此成员。如果你这样做,你的代码将 抛出一个 MethodAccessException。这个成员是安全关键的,它 将其限制为 Silverlight 的 .NET Framework 内部使用 类库。 [安全关键]

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 2011-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多