我有一个 SilverLight 库 (dll),它实现了我需要在 .net 项目中使用的类。我遵循了这个答案中描述的步骤,但我没有看到如何使用代理库来公开我需要的类。我在 .net 项目中添加了对代理的引用。该dll适用于silverlight 5,我使用的是.net 4.0。
如果我只是添加 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)