2010-10-28 45 views
0

今天早上我遇到了一個與我的Silverlight 4應用程序不同尋常的問題。我一直是IoC框架的忠實粉絲,因此決定將Unity用於我的項目。但只要我將程序集添加到我的項目中,就會出現以下錯誤。向Silverlight 4項目添加Unity dll參考

System.Windows.Markup.XamlParseException occurred 

Message=Could not load type 'System.Security.AllowPartiallyTrustedCallersAttribute' from assembly 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. 

StackTrace: at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) 
      at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) 
      at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) 
      at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) 
      at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) 
      at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean isDecoratedTargetSecurityTransparent) 
      at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) 
      at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit) 
      at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit) 
      at MS.Internal.XamlSchemaContext.ProcessXmlnsDefinitions(Assembly assembly, String assemblyName) 
      at MS.Internal.XamlSchemaContext.EnsureManagedAssemblyAttributesLoaded() 
     InnerException: 

現在,如果我刪除DLL我的應用程序工作絕對沒問題。自從早上起,我一直在撓頭,並沒有太多的運氣與StackOverflow搜索或谷歌。

任何幫助,非常感謝。

乾杯---捷豹

+0

您是否使用Unity for Silverlight(http://msdn.microsoft.com/en-us/library/ff678312.aspx)?我在Prism框架內使用Unity(http://compositewpf.codeplex.com/),我從來沒有遇到過這個問題。 – Murven 2010-10-28 13:31:38

+0

是的,我正在使用適用於Silverlight的Unity 2.0。我實際上已將dll更改爲Laurent Bugnion示例中使用的dll,並且它似乎工作正常。顯然,新Unity dll存在問題。但我只是感到驚訝沒有人面臨這個問題!希望我不在這裏愚蠢:-) – Jag 2010-10-28 14:00:25

+0

看看這是否有所幫助:http://stackoverflow.com/questions/2914017/problem-with-conversion-of-existing-project-to-silverlight-4/9167878#9167878 – 2012-02-06 21:59:43

回答

0

This article可能會提供一些有用的信息。它基本上是說:

「在 部分信任環境中使用統一時限制是 不能註冊和使用 的RegisterType方法使用映射在 目標類是內部的(C#),朋友 (Visual Basic .NET),私有(C#)或 私有(Visual Basic .NET)。「

+0

嗨達米安,謝謝你的回覆。我沒有寫任何統一碼。我所做的只是添加參考和應用程序爆炸:-( – Jag 2010-10-28 13:55:36

1

請確認您確實引用了Silverlight程序集而不是桌面程序。 AllowPartiallyTrustedCallersAttribute類不在Silverlight版本中。

您使用的是Unity 2.0嗎?如果是這樣,該DLL被命名爲「Microsoft.Practices.Unity.Silverlight.dll」。這是你添加的DLL嗎?如果「.Silverlight」不在文件名中,那麼你就是在引用桌面dll。

0

做一個乾淨的構建,然後完全重建爲我解決了這個問題。