为什么 Assembly.GetCustomAttributes 突然在使用 Silverlight 4 的构建计算机上抛出 TypeLoadException?

发布于 2024-09-01 10:04:57 字数 2360 浏览 11 评论 0原文

不久前,我必须显示 Silverlight 应用程序的当前版本。经过一番谷歌搜索后,以下代码给了我想要的结果:

var fileVersionAttributes = typeof(MyClass).Assembly.
    GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false) as AssemblyFileVersionAttribute[];
var version = fileVersionAttributes[0].Version;

这在我们的 .NET 3.5 Silverlight 3 环境中很有效。然而,我们最近升级到了 .NET 4 和 Silverlight 4。我们刚刚完成构建机器的工作,发现此代码的单元测试抛出以下异常:

Exception Message: 

System.TypeLoadException: Error 0x80131522.  Debugging resource strings are unavailable. See http://go.microsoft.com/fwlink/?linkid=106663&Version=3.0.50106.0&File=mscorrc.dll&Key=0x80131522 
   at System.ModuleHandle.ResolveType(ModuleHandle module, Int32 typeToken, RuntimeTypeHandle* typeInstArgs, Int32 typeInstCount, RuntimeTypeHandle* methodInstArgs, Int32 methodInstCount)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.Module.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, RuntimeMethodHandle& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.GetCustomAttributes(Module decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(Module decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(Assembly assembly, Type caType)
   at System.Reflection.Assembly.GetCustomAttributes(Type attributeType, Boolean inherit)
   at MyCode.VersionTest()

我以前从未见过此异常,并且其中的链接无处指向。它只是扔在构建机器上而不是我的开发盒子上,所以我正在经历一个反复试验的过程,以查看两者之间的任何差异。

知道为什么会发生这种情况吗?

干杯, 安德烈.

A short while back i had to display the current version of our Silverlight app. After some googling the following code gave me the desired result:

var fileVersionAttributes = typeof(MyClass).Assembly.
    GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false) as AssemblyFileVersionAttribute[];
var version = fileVersionAttributes[0].Version;

This worked a treat in our .NET 3.5 Silverlight 3 environment. However, we recently upgraded to .NET 4 and Silverlight 4. We just finished getting our build machine working and found that the unit test for this code was throwing the following exception:

Exception Message: 

System.TypeLoadException: Error 0x80131522.  Debugging resource strings are unavailable. See http://go.microsoft.com/fwlink/?linkid=106663&Version=3.0.50106.0&File=mscorrc.dll&Key=0x80131522 
   at System.ModuleHandle.ResolveType(ModuleHandle module, Int32 typeToken, RuntimeTypeHandle* typeInstArgs, Int32 typeInstCount, RuntimeTypeHandle* methodInstArgs, Int32 methodInstCount)
   at System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
   at System.Reflection.Module.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
   at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, RuntimeMethodHandle& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
   at System.Reflection.CustomAttribute.GetCustomAttributes(Module decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(Module decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean isDecoratedTargetSecurityTransparent)
   at System.Reflection.CustomAttribute.GetCustomAttributes(Assembly assembly, Type caType)
   at System.Reflection.Assembly.GetCustomAttributes(Type attributeType, Boolean inherit)
   at MyCode.VersionTest()

I have never seen this exception before and the link in it points nowhere. It is only throwing on the build machine and not on my development box, so i'm going through a process of trial and error to see any differences between the two.

Any idea why this might be happening??

Cheers,
Andrej.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2024-09-08 10:04:58

通常,当 Silverlight 程序集链接到桌面库并且其类型之一访问 Silverlight 不支持的系统类型时,您会看到 TypeLoadException

我无法真正解释为什么它在 Silverlight 4 下崩溃,但我猜它是你的 silverlight 库中的东西。您可以在一种系统类型上执行相同的代码并查看它是否崩溃吗?

typeof(string).Assembly.
    GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false) as AssemblyFileVersionAttribute[];

如果上面的代码没有崩溃,请开始注释掉 MyClass 的部分,以查看它的哪一部分导致 TypeLoadException

Usually you would see TypeLoadException when a Silverlight assembly has been linked to desktop libraries and one its types accesses a system type that is not supported on Silverlight.

I can't really explain why it broke under Silverlight 4, but my guess it's something inside your silverlight library. Can you execute the same code on one of system types and see if it crashes?

typeof(string).Assembly.
    GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false) as AssemblyFileVersionAttribute[];

If the above doesn't crash, start commenting out bits of MyClass to see which part of it causes TypeLoadException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文