2012-08-31 77 views
2

我有一个SilverLight lib(dll),它实现了需要在.net项目中使用的类。我详细描述了in this answer中描述的步骤,但我没有看到如何使用代理库来公开我需要的类。我在.net项目中添加了对代理的引用。该DLL是为silverlight 5和我使用.net 4.0。在.net项目中使用SilverLight库(dll)

如果我只是添加dll作为参考,我可以使用SL对象,但我不能序列化它们。

这里是有问题的代码:

static string SerializeWithDCS(object obj) 
    { 
     if (obj == null) throw new ArgumentNullException("obj"); 
     DataContractSerializer dcs = new DataContractSerializer(obj.GetType()); 
     MemoryStream ms = new MemoryStream(); 
     dcs.WriteObject(ms, obj); 
     return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position); 
    } 

序列化失败:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.Serialization, Versio 
n=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system ca 
nnot find the file specified.                   
File name: 'System.Runtime.Serialization, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea 
7798e'                         
    at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int 
32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)    
    at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeType 
Handle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)      
    at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Ty 
pe[] genericMethodArguments)                   
    at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, Me 
tadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decora 
tedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList deriv 
edAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolea 
n& isVarArg)                       
    at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32 
decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable)     
    at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inheri 
t)                          
    at System.RuntimeType.IsDefined(Type attributeType, Boolean inherit)        
    at System.Runtime.Serialization.CollectionDataContract.IsCollectionOrTryCreate(Type type, Boolean t 
ryCreate, DataContract& dataContract, Type& itemType, Boolean constructorRequired)      
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id 
, RuntimeTypeHandle typeHandle, Type type)                
    at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidati 
on(Int32 id, RuntimeTypeHandle typeHandle, Type type)  
+0

您可能遇到了与此[问题](http://stackoverflow.com/q/7266198/650012)和[答案](http://stackoverflow.com/a/7274273/650012)中相同的问题。 Woudl可以创建.NET库并包含Silverlight源代码吗? –

+0

您是否尝试将Silverlight项目转换为可移植类库? http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981/ - 另一种选择是项目链接器:http://visualstudiogallery.msdn.microsoft.com/5e730577-d11c-4f2e-8e2b- cbb87f76c044 – herzmeister

+0

@AndersGustafsson:我没有资料。 – user1367401

回答

0

使用反射编写自己的序列。

这是我能想出的最好的解决方案 - 可能是一个更好的人提供了一个优越的答案。

这个问题似乎确实是System.Runtime.Serialization中.NET和Silverlight不同的属性。在与Anders Gustafsson有关的问题中讨论了这个问题。

我很困惑,为什么我不能得到这个工作。

我试图创建一个AppDomain和所有必要的组件加载到其中:

  1. 测试DLL冒充第三方组件,
  2. Silverlight的包装程序,将做系列化,
  3. 所有Silverlight 5核心组件和
  4. System.Runtime.Serialization的Silverlight 5变体。

然后我调用封装程序集中的代码来调用序列化 - 我仍然有例外。

这令人费解 - 也许我只是做了错误的事情。

如果有人对此有更多的了解,我会非常感兴趣。

无论如何,它可能写一个自定义序列化程序,我认为这将解决问题。