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()

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

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

干杯,安德烈。

4

1 回答 1

0

通常,您会看到TypeLoadExceptionSilverlight 程序集何时链接到桌面库,并且它的一个类型访问了 Silverlight 不支持的系统类型。

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

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

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

于 2010-05-11T02:56:57.847 回答