【问题标题】:Why is Assembly.GetCustomAttributes suddenly throwing TypeLoadException on build machine with Silverlight 4?为什么 Assembly.GetCustomAttributes 在使用 Silverlight 4 的构建机器上突然抛出 TypeLoadException?
【发布时间】:2011-02-17 23:06:44
【问题描述】:

不久前,我不得不展示我们 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()

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

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

干杯, 安德烈。

【问题讨论】:

    标签: c# continuous-integration silverlight-4.0 typeloadexception assemblyversionattribute


    【解决方案1】:

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

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

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

    如果上面没有崩溃,开始注释掉MyClass的位,看看是哪个部分导致TypeLoadException

    【讨论】:

      猜你喜欢
      • 2012-10-26
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      相关资源
      最近更新 更多