2010-10-28 62 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

做一个干净的构建,然后完全重建为我解决了这个问题。